Private Sub Command1_Click()     '写数据到文件
  Open "c:\stu.txt" For Output As #1
  Write #1, Text1.Text, Text2.Text, Text3.Text, Text4.Text
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Close #1
End SubPrivate Sub Command2_Click()      '读出刚写进的数据
Open "c:\stu.txt" For Input As #1
Dim a As String
Dim b As Integer
Dim c As Single
Dim d As Date
     Input #1, a, b, c, d
    Text1.Text = a
    Text2.Text = CInt(b)
     Text3.Text = CSng(c)
     Text4.Text = CDate(d)
Close #1
End SubPrivate Sub Command3_Click()    '查看数据类型
Dim a, b, c, d
a = Text1.Text
b = Text2.Text
c = Text3.Text
d = Text4.Text
Form1.Print "type", VarType(a), VarType(b), VarType(c), VarType(d),
'Form1.Print "type", VarType(Text1.Text), VarType(Text2.Text), VarType(Text3.Text),  VarType(Text4.Text)
End Sub
问题之这样的,文本框1输入字符型,文本框2输入整型,文本框3输入单精度型,文本框4输入日期型
比如输入:文本框1:sshh ,文本框2:11,文本框3: 12.32,文本框4:99-9-8          
在C盘下stu.txt文件下能看到:"sshh","11","12.32","99-9-8"
但是再按command2时,除了文本框1显示sshh外,其他的都不是原来输入的了,框2和框3都是0,框4是0:00:00,请问为什么会这样?
我用command3查了输出的数据的类型,结果显示都是字符串,
怎么才能使a,b,c,d是原来的类型呢?a——字符串,b——整型,c——单精度,d——日期型??

解决方案 »

  1.   

    前面的text1.text都有问题,没有处理就write的话,全部当成string写入的文件。
    所以每个字边上都有""号。  正常的话 数字 边上是没有 ""号的。所以读取的时候dim a as integer 肯定不能把当作string写入文件的"11"读出来。类型不匹配。后面都是这个原因。试试Private Sub Command1_Click()     'дÊý¾Ýµ½Îļþ
        Open "c:\stu.txt" For Output As #1
        Write #1, Text1.Text, CInt(Text2.Text), CSng(Text3.Text), CDate(Text4.Text)
        Text1.Text = ""
        Text2.Text = ""
        Text3.Text = ""
        Text4.Text = ""
        Close #1
    End SubPrivate Sub Command2_Click()      '¶Á³ö¸Õд½øµÄÊý¾Ý
        Open "c:\stu.txt" For Input As #1
        Dim a As String
        Dim b As Integer
        Dim c As Single
        Dim d As Date
         Input #1, a, b, c, d
        Text1.Text = a
        Text2.Text = b
            Text3.Text = c
            Text4.Text = d
        Close #1
    End Sub
      

  2.   

    还有一种就是第2个command里面不要固定了变量的类型。Private Sub Command1_Click()
        Open "c:\stu.txt" For Output As #1
        Write #1, Text1.Text, Text2.Text, Text3.Text, Text4.Text
        Text1.Text = ""
        Text2.Text = ""
        Text3.Text = ""
        Text4.Text = ""
        Close #1
    End SubPrivate Sub Command2_Click()
        Open "c:\stu.txt" For Input As #1
        Dim a, b, c, d
         Input #1, a, b, c, d
        Text1.Text = a
        Text2.Text = b
            Text3.Text = c
            Text4.Text = d
        Close #1
    End Sub
      

  3.   

    Private Sub Command1_Click()     '写数据到文件
        Open "c:\stu.txt" For Output As #1
        Write #1, Text1.Text, CInt(Text2.Text), CDbl(Text3.Text), CDate(Text4.Text)
        Text1.Text = ""
        Text2.Text = ""
        Text3.Text = ""
        Text4.Text = ""
        Close #1
    End SubPrivate Sub Command2_Click()      '读出刚写进的数据
        Open "c:\stu.txt" For Input As #1
        Dim a As String
        Dim b As Integer
        Dim c As Single
        Dim d As Date     Input #1, a, b, c, d
         
        Text1.Text = a
        Text2.Text = b
        Text3.Text = c
        Text4.Text = d
        
        Close #1
    End Sub这样就没问题的~
      

  4.   

    按LBwu和gdami的第一种的写法。可以正确读到文本框,及写进的数据都可以原样读出来
    我现在想把这些数据读入数据库,
    加入如下代码
    Private Sub Form_Load()
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db2.mdb"
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db2.mdb"
    End Sub在command2_click() 中
    读出数据后加语句
    Private Sub Command2_Click()      '读出刚写进的数据
        Open "c:\stu.txt" For Input As #1
        Dim a As String
        Dim b As Integer
        Dim c As Single
        Dim d As Date     Input #1, a, b, c, d
         
        Text1.Text = a
        Text2.Text = b
        Text3.Text = c
        Text4.Text = d
    cn.Execute "insert into 表1(a,b,c,d) values('" & a & "'," & b & "," & c & "," & b & ")"
        Close #1
    End Sub结果写到数据库日期型的有问题,总是出现与输入不相符的日期,而且毫无规律,比如输入的2002-2-1,或者1998-02-14,数据库中会出现1901年1月2日等等,各位高手遇到过这样的情况吗?这是为什么??
    数据库字段a是字符型
      b是整型
      c是单精度
              d是日期型,长日期型
      

  5.   

    数值型写入时可这样转换:format(数值,"0")       '[视你要求的类型]
    日期型写入时可这样转换:format(日期,"yyyy-mm-dd")读出时可这样转换:
    数值型:val(数值字符串变量)
           cint(数值字符串变量)
           clng(数值字符串变量)
           ...[视类型]
    日期型:cdate(日期型字符串变量)