往文件中写入数据,当是英文名称时,不会出错 ,当输入中文名时,Text4.text会显示不出来,不知什么原因,请高手指教,谢谢!!
Private Type Product
    Name As String * 10
    Price As Integer
    Number As Integer
End TypePrivate Sub Command1_Click()
 
Static count As Integer
Dim prod As ProductWith prod
    .Name = Text1.Text
    .Price = Val(Text2.Text)
    .Number = Val(Text3.Text)
End Withcount = count + 1
Open App.Path & "\product.dat" For Random As #1
Put #1, count, prod
Close #1Text1.Text = ""
Text2.Text = ""
Text3.Text = ""End Sub
Private Sub Command2_Click()
Dim i As Integer
Dim n As Integer
Dim prod As ProductOpen App.Path & "\product.dat" For Random As #1
n = LOF(1) / Len(prod)For i = 1 To n
    Get #1, i, prod
    Debug.Print Trim(prod.Price)
    If Trim(prod.Name) = Trim(Text5.Text) Then
        Text4.Text = "产品:" & prod.Name & Chr(13) & Chr(10) & "数量:" & prod.Number & Chr(13) & Chr(10) & "单价:" & prod.Price
        
        'Text3.Text = prod.Price
    End If
    
Next iClose #1End Sub界面如下:

解决方案 »

  1.   

    With prod
    .Name = Text1.Text
    .Price = Val(Text2.Text)
    .Number = Val(Text3.Text)去掉val
      

  2.   

      道理很简单:一个汉字占2个字节,但它计算‘记录长度’时,字符串是按字符数来算的,然后它又直接把这个长度当成字节数来处理。这样,只要出现了汉字,它后面的成员位置以及其它‘记录’在输入时,就出现偏差(文件偏移量偏差),当然就不能正确读取了。  参考我的这段代码处理吧,自己多想,别再问我为什么了:
    Option Explicit'Private Type Product
    'Name As String * 10
    'Price As Integer
    'Number As Integer
    'End Type
    Private Const NAMELEN   As Long = 30    'Name字段字节数
    Private Type ProductForOut
        strName As String
        Price As Integer
        Number As Integer
    End Type
    Private Type ProductForInput
        NA As Integer               '用于吃掉多余的信息
        bName(NAMELEN - 1) As Byte  '因为默认下标从0开始
        Price As Integer
        Number As Integer
    End TypePrivate Sub Command1_Click()    Static count As Integer
        'Dim prod As Product
        Dim prod As ProductForOut
        Dim strTemp$, i&
        
        strTemp = Text1.Text
        i = LenB(StrConv(Text1.Text, vbFromUnicode))
        If (i > 20) Then
            MsgBox "Name字段输入过长!", 16, "错误"
            Exit Sub
        End If
        With prod
            .strName = strTemp & Space$(NAMELEN - i)
            .Price = Val(Text2.Text)
            .Number = Val(Text3.Text)
        End With
        count = count + 1
        Open App.Path & "\product.dat" For Random As #1
        Put #1, count, prod
        Close #1
        
        Text1.Text = ""
        Text2.Text = ""
        Text3.Text = ""End SubPrivate Sub Command2_Click()
        Dim i As Integer
        Dim n As Integer
        'Dim prod As Product
        Dim prod As ProductForInput
        Dim strTemp$    Open App.Path & "\product.dat" For Random As #1
        n = LOF(1) / Len(prod)
        For i = 1 To n
            Get #1, i, prod
            Debug.Print Trim(prod.Price)
            strTemp = StrConv(prod.bName, vbUnicode)
            If (Trim(strTemp) = Trim(Text5.Text)) Then
                Text4.Text = "产品:" & strTemp & vbCrLf & "数量:" & prod.Number & vbCrLf & "单价:" & prod.Price
            End If
        Next i
        Close #1End Sub
      

  3.   

    If Trim(prod.Name) = Trim(Text5.Text) Then
    在这行下面加上 msgbox "aaa" 确认上面的等式已成立上面的等式如不成立原因可能有二1.改为 If Ucase(Trim(prod.Name)) = Ucase(Trim(Text5.Text)) Then
    VB 的 "ABC" 是不等于 "abc" 的2.Name As String * 10
    改为 * 20 因为一个汉字等于英文字的两倍 有可能出错 或取到的内容长度不对
      

  4.   

    ASCII码和GBK的区别,汉字占用2个字节
      

  5.   

    另外:输出到文件时,建议使用write #1,变量名 这个方法.
    只是变量名要定义好类型,如果是variant的话,存进去的内容会带变量类型,不是你想要的结果.
      

  6.   

    command2_click中的增加一句:
    prod.Name = Trim(Replace(prod.Name, Chr(0), ""))
    If Trim(prod.Name) = Trim(Text5.Text) Then