| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1399 人关注过本帖
标题:空值怎么往ACCESS数据库里填写?
只看楼主 加入收藏
ictest
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:333
专家分:114
注 册:2010-2-17
结帖率:70%
收藏
已结贴  问题点数:10 回复次数:2 
空值怎么往ACCESS数据库里填写?
我用如下语句生成了一个TXT文件——11.txt,现在想将这个文件里的内容填写到ACCESS数据库里,生成TXT文件时,某些行的固定位置为空值,再输入到数据库的某列(数据类型为双精度数字)时,报“实时错误 3421 数据类型转换错误”,TXT文件报错的行是第35行,请问如何修改?我使用了几种方式写TXT文件,都是当写入空值时报错。
方式一:
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(10) & " " & "Null" & " " & b(11) & " "
方式二:
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(10) & " " & " " & " " & b(11) & " "
方式三:
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(10) & " " & " " & b(11) & " "

生成TXT文件语句:
程序代码:
Dim a As String
Dim N As Integer, iPos As Integer
flag1_HB_3 = False
flag2_HB_3 = False
FileRead_HB_3 = FreeFile
Open (App.Path & "\导出\") & "temp2.txt" For Input As FileRead_HB_3   'Dir1.Path & "\"  & "testfile.txt"
FileWrite_HB_3 = FreeFile
Open (App.Path & "\导出\") & Text2(i_total).Text & ".txt" For Output As FileWrite_HB_3
Do While Not EOF(FileRead_HB_3)
Line Input #FileRead_HB_3, temp_HB_3

If (InStr(temp_HB_3, "<") <> 0) Then
N = 0
For iPos = 1 To Len(temp_HB_3)
If Mid(temp_HB_3, iPos, Len("<=")) = "<=" Then
N = N + 1
End If
Next

b() = Split(temp_HB_3, " ")
flag1_HB_3 = True
flag2_HB_3 = False

If N = 2 And b(8) = "<=" Then
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(9) & " " & b(11) & " " & "Null" & " "
End If

If N = 2 And b(9) = "<=" Then
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(10) & " " & b(13) & " " & b(14) & " "
End If

If N = 1 And b(9) = "<=" Then
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(10) & " " & "Null" & " " & b(11) & " "
End If

If N = 1 And b(8) = "<=" Then
Print #FileWrite_HB_3, Text2(i_total).Text & " " & b(0) & " " & b(3) & " " & b(4) & " " & b(6) & " " & b(7) & " " & b(9) & " " & "Null" & " " & "Null" & " "
End If

End If
Loop
Close FileRead_HB
Close FileWrite_HB


写入数据库语句:
程序代码:
TxtFile = (App.Path & "\导出\") & Text2(i_total).Text & ".txt" '文件文件位置
DbFile = (App.Path & "\" & "variation.mdb")    '数据库文件位置
Set myDB = OpenDatabase(DbFile)    '打开数据库文件
Set myTB = myDB.OpenRecordset("variation")    '打开数据库表1
Open TxtFile For Input As #1  '打开文本文件
Do While Not EOF(1)
    Line Input #1, StrTemp
    StrSp = Split(StrTemp, " ")
    myTB.AddNew
    myTB.Fields("文件名") = StrSp(0)
    myTB.Fields("管芯编号") = StrSp(1)
    myTB.Fields("测试组别") = StrSp(2)
    myTB.Fields("测试项目") = StrSp(3)
    myTB.Fields("管脚号") = StrSp(4)
    myTB.Fields("测试值下限") = StrSp(5)
    myTB.Fields("测试值") = StrSp(6)
    myTB.Fields("测试值上限") = StrSp(7)
    myTB.Fields("单位") = StrSp(8)
    
    myTB.Update
Loop
Close #1
myTB.Close
myDB.Close


报错行:
    myTB.Fields("测试值上限") = StrSp(7)

txt文件和数据库

11.rar (17.82 KB)
variation.rar (1.14 MB)
搜索更多相关主题的帖子: txt 文件 Text If Fields 
2018-05-29 11:02
wmf2014
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:216
帖 子:2039
专家分:11273
注 册:2014-12-6
收藏
得分:10 
如果填入字段是数值型的,应该写成“myTB.Fields("测试值上限") = val(StrSp(7))”
如果是字符型的,就写成“if StrSp(7)<>"" then myTB.Fields("测试值上限") = StrSp(7)”

能编个毛线衣吗?
2018-05-29 11:08
ictest
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:333
专家分:114
注 册:2010-2-17
收藏
得分:0 
谢谢wmf2014版主!
我用您教的“myTB.Fields("测试值上限") = val(StrSp(7))”方式解决问题了!
再次感谢!
2018-05-31 09:40
快速回复:空值怎么往ACCESS数据库里填写?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019925 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved