本人是初学VB,其他部分已经搞完,就是EXCEL比对导入SQL这块困扰了好久。
下面这段是我将SQL导出EXCEL编码反过来思考得到的结果,希望大家不要笑话我!谢谢
这个模块我希望实现的目标:
1,将指定目录下的一个EXCEL文件中的数据,与SQL中的两个数据库进行比对。(帐号和统测号两个字段必须都核对相符)
2,记录存在的,将“总库”中的记录“标记”字段改为3,将“送盘库”中的记录删除。
3,记录不存在的,报个错,生成个LOG文件,继续比对下去。 问题:
1,运行这段代码会出错,Set xlBook = xlApp.Workbooks.Count("text1.text")这里会报“需要对象”
2,怎样才能做一个“浏览”按钮,让我自己选择EXCEL文件的位置?然后将得到的路径变成TEXT1的值?
Dim j As Integer
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet Private Sub from_load()
Text2.Text = 3
End Sub
Private Sub cmdok_Click()
If Text1.Text = "" Then
MsgBox ("请选择批量中止文件位置!"), vbOKOnly
Else
Set xlBook = xlApp.Workbooks.Count("text1.text")
Set xlSheet = xlBook.Worksheets(1)
For j = 2 To xlSheet.Rows - 1
Adodc2.RecordSource = "select * from 总库 where 统册号='" + xlSheet.Cells(j + 1, 2) + "' & 帐号='" + xlSheet.Cells(j + 1, 1) + "'"
Adodc2.Refresh
Adodc1.RecordSource = "select * from 送盘库 where 统册号='" + xlSheet.Cells(j + 1, 2) + "' & 帐号='" + xlSheet.Cells(j + 1, 1) + "'"
Adodc1.Refresh
If Adodc2.Recordset.RecordCount = 0 Or Adodc1.Recordset.RecordCount = 0 Then MsgBox ("没有统册号:xlSheet.Cells(j + 1, 2),帐号:xlSheet.Cells(j + 1, 1),的记录!"), vbOKOnly
Next j
If Adodc2.Recordset.Fields("统册号") <> "" & Adodc2.Recordset.Fields("帐号") <> "" Then
Adodc2.Recordset.Fields("变更日期") = Trim(xlSheet.Cells(j + 1, 5))
Adodc2.Recordset.Fields("备注") = Trim(xlSheet.Cells(j + 1, 6))
Adodc2.Recordset.Fields("标志") = Val(Text2.Text)
Adodc2.Recordset.Update
End If
If Adodc1.Recordset.Fields("统册号") <> "" & Adodc2.Recordset.Fields("帐号") <> "" Then
Adodc1.Recordset.Delete
Adodc1.Refresh
MsgBox ("批量中止授权成功!")
End If
End If
End Sub
Private Sub cmdcancel_Click()
Unload Me
End Sub
下面这段是我将SQL导出EXCEL编码反过来思考得到的结果,希望大家不要笑话我!谢谢
这个模块我希望实现的目标:
1,将指定目录下的一个EXCEL文件中的数据,与SQL中的两个数据库进行比对。(帐号和统测号两个字段必须都核对相符)
2,记录存在的,将“总库”中的记录“标记”字段改为3,将“送盘库”中的记录删除。
3,记录不存在的,报个错,生成个LOG文件,继续比对下去。 问题:
1,运行这段代码会出错,Set xlBook = xlApp.Workbooks.Count("text1.text")这里会报“需要对象”
2,怎样才能做一个“浏览”按钮,让我自己选择EXCEL文件的位置?然后将得到的路径变成TEXT1的值?
Dim j As Integer
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet Private Sub from_load()
Text2.Text = 3
End Sub
Private Sub cmdok_Click()
If Text1.Text = "" Then
MsgBox ("请选择批量中止文件位置!"), vbOKOnly
Else
Set xlBook = xlApp.Workbooks.Count("text1.text")
Set xlSheet = xlBook.Worksheets(1)
For j = 2 To xlSheet.Rows - 1
Adodc2.RecordSource = "select * from 总库 where 统册号='" + xlSheet.Cells(j + 1, 2) + "' & 帐号='" + xlSheet.Cells(j + 1, 1) + "'"
Adodc2.Refresh
Adodc1.RecordSource = "select * from 送盘库 where 统册号='" + xlSheet.Cells(j + 1, 2) + "' & 帐号='" + xlSheet.Cells(j + 1, 1) + "'"
Adodc1.Refresh
If Adodc2.Recordset.RecordCount = 0 Or Adodc1.Recordset.RecordCount = 0 Then MsgBox ("没有统册号:xlSheet.Cells(j + 1, 2),帐号:xlSheet.Cells(j + 1, 1),的记录!"), vbOKOnly
Next j
If Adodc2.Recordset.Fields("统册号") <> "" & Adodc2.Recordset.Fields("帐号") <> "" Then
Adodc2.Recordset.Fields("变更日期") = Trim(xlSheet.Cells(j + 1, 5))
Adodc2.Recordset.Fields("备注") = Trim(xlSheet.Cells(j + 1, 6))
Adodc2.Recordset.Fields("标志") = Val(Text2.Text)
Adodc2.Recordset.Update
End If
If Adodc1.Recordset.Fields("统册号") <> "" & Adodc2.Recordset.Fields("帐号") <> "" Then
Adodc1.Recordset.Delete
Adodc1.Refresh
MsgBox ("批量中止授权成功!")
End If
End If
End Sub
Private Sub cmdcancel_Click()
Unload Me
End Sub
------------------------------------------------------------------------------------------
在你的浏览按钮的Click事件中添加以下代码: Dim fd As New OpenFileDialog fd.Filter = "Excel File(*.xls)|*.xls"
fd.FileName = ""
fd.ShowDialog()
If fd.FileName <> "" Then
TextBox1.Text = fd.FileName
End If
fd.Dispose()
因为我用的是VB6,所以openfiledialog不能用
后来去查了下资料,改用dirlistbox、driverlistbox、filelistbox就OK了!早上出去跑了2个DB店,居然没有VB2005卖,郁闷!
然后是“set xlbook = xlapp.workbooks.count("text1.text")"使用有问题,改成workbooks.open就OK现在变成出错在“for j = 2 to xlsheet.rows - 1”,内存溢出……
我去找找2005哪里有下载。但是后面那段比较和修改的代码,错误的地方实在太多,估计要重写了,诶~