使用VB6.0,将excel表中数据插入Access数据库后,部分字段的内容出现丢失,不知道是为什么。下面是我的程序,被插入的表为“台账”,插入后“台账”中的字段“供方代码”和“图号”均出现部分内容丢失,而字段“供方名称”、“故障描述”等均是完整的。
Public dbConnectionString As String
Public excelConnectionString As String
Dim rstexcel As ADODB.Recordset
Dim rstdb As ADODB.Recordset
Dim txtsql1 As String
Dim txtSql As String
Public Function GetConn() As Boolean
pathMainExcel = App.Path & "\test.xls"
Set excelObj = New ADODB.Connection
excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathMainExcel & ";Extended Properties='Excel 8.0';"
excelObj.Open excelConnectionString
End Function
Public Function GetDb() As Boolean
pathMaindb = App.Path & "\test.mdb"
Set dbobj = New ADODB.Connection
dbConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathMaindb & ";"
dbobj.Open dbConnectionString
End Function
Private Sub Command1_Click()
Call GetConn ''调用excel 的数据库连接
Call GetDb ''调用access的数据库连接
Set rstexcel = New ADODB.Recordset
txtSql = "select * from [sheet1$]" ''从sheet1中读取数据
rstexcel.Open txtSql, excelObj, adOpenDynamic, adLockOptimistic
Dim sint As Integer
Set rstdb = New ADODB.Recordset
txtsql1 = "select * from 台账"
rstdb.Open txtsql1, dbobj, adOpenDynamic, adLockOptimistic
Do Until rstexcel.EOF
rstdb.AddNew ''''往数据库中添加数据
rstdb("序号") = rstexcel("序号")
rstdb("图号") = rstexcel("图号")
rstdb("零件名称") = rstexcel("零件名称")
rstdb("供方名称") = rstexcel("供方名称")
rstdb("供方代码") = rstexcel("供方代码")
' If rstdb("供方代码") = "" Then rstdb("供方代码") = "供方代码为空"
rstdb("故障描述") = rstexcel("故障描述")
rstdb("出处") = rstexcel("出处")
rstdb("接收日期") = rstexcel("接收日期")
rstdb("备注") = rstexcel("备注")
rstdb.Update
rstexcel.MoveNext
Loop
rstdb.Close
Command1.Enabled = False ''将按钮设置位不可用防止数据重复导入
End Sub
字段长度均足够,且将数据类型改为货币型后,问题还是这样。