vb6.0 Excel数据导入Access不能任意路径
vb6.0Excel数据导入Access不能任意路径,必须要EXCEL文件和ACCESS数据库在同一个目录下才能成功导入,请大神帮忙看看怎么修改一下才能EXCEL在任意位...
vb6.0 Excel数据导入Access不能任意路径,必须要EXCEL文件和ACCESS数据库在同一个目录下才能成功导入,请大神帮忙看看怎么修改一下才能EXCEL在任意位置都可以导入。
----------------------------打开数据库找到数据表部分------------------
Private Sub Form_Load() Dim cnn1 As New ADODB.Connection Dim rstSchema As New ADODB.Recordset Command1.Enabled = False Command2.Enabled = False dataBaseName = "data.mdb" strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\data.mdb" cnn1.Open strCnn Set rstSchema = cnn1.OpenSchema(adSchemaTables) If (Not rstSchema.EOF) Then If rstSchema!TABLE_TYPE = "TABLE" Then sjbm = rstSchema!TABLE_NAME End If End If rstSchema.Close cnn1.Close Set rstSchema = Nothing Set cnn1 = NothingEnd Sub
----------------------------打开数据库找到数据表部分------------------
---------------导入部分-------------
Private Sub Command2_Click() If Text1.Text = "" Then MsgBox "亲,请加载正确的Excel文件!", vbCritical, "文件未找到" Exit Sub End If On Error GoTo ErrorHandler ' 打开错误处理程序 sql = "insert INTO " & sjbm & " IN '" & dataBaseName & "' select * from [data$]" Conn.Execute (sql) MsgBox "数据导入成功~~~╰( ̄▽ ̄)╮", vbInformation, "信息提示" Command1.Enabled = False Command2.Enabled = False Exit SubErrorHandler: ' 错误处理程序 MsgBox Err.DescriptionEnd Sub
---------------------------------
导入不同文件夹的EXCEL文件会提示找不到数据库文件,我就没懂怎么回事,不知道是那里的地址错了。
只有EXCEL和数据库在同文件夹才能导入成功。请教~~~~~
连接EXCEL的部分代码
If CDlg.FileName = "" Then Exit Sub
Text1.Text = CDlg.FileName
If Conn.State = adStateOpen Then Conn.Close
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & CDlg.FileName & ";Extended Properties='Excel 8.0;HDR=Yes'" '连接EXCEL文件
sql = "select * from [data$]" '打开EXCEL表
rs.CursorLocation = adUseClient
rs.Open sql, Conn, 3, 3
Label1.Caption = "共有记录: " & rs.RecordCount
Set DataGrid1.DataSource = rs
End Sub 展开
----------------------------打开数据库找到数据表部分------------------
Private Sub Form_Load() Dim cnn1 As New ADODB.Connection Dim rstSchema As New ADODB.Recordset Command1.Enabled = False Command2.Enabled = False dataBaseName = "data.mdb" strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\data.mdb" cnn1.Open strCnn Set rstSchema = cnn1.OpenSchema(adSchemaTables) If (Not rstSchema.EOF) Then If rstSchema!TABLE_TYPE = "TABLE" Then sjbm = rstSchema!TABLE_NAME End If End If rstSchema.Close cnn1.Close Set rstSchema = Nothing Set cnn1 = NothingEnd Sub
----------------------------打开数据库找到数据表部分------------------
---------------导入部分-------------
Private Sub Command2_Click() If Text1.Text = "" Then MsgBox "亲,请加载正确的Excel文件!", vbCritical, "文件未找到" Exit Sub End If On Error GoTo ErrorHandler ' 打开错误处理程序 sql = "insert INTO " & sjbm & " IN '" & dataBaseName & "' select * from [data$]" Conn.Execute (sql) MsgBox "数据导入成功~~~╰( ̄▽ ̄)╮", vbInformation, "信息提示" Command1.Enabled = False Command2.Enabled = False Exit SubErrorHandler: ' 错误处理程序 MsgBox Err.DescriptionEnd Sub
---------------------------------
导入不同文件夹的EXCEL文件会提示找不到数据库文件,我就没懂怎么回事,不知道是那里的地址错了。
只有EXCEL和数据库在同文件夹才能导入成功。请教~~~~~
连接EXCEL的部分代码
If CDlg.FileName = "" Then Exit Sub
Text1.Text = CDlg.FileName
If Conn.State = adStateOpen Then Conn.Close
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & CDlg.FileName & ";Extended Properties='Excel 8.0;HDR=Yes'" '连接EXCEL文件
sql = "select * from [data$]" '打开EXCEL表
rs.CursorLocation = adUseClient
rs.Open sql, Conn, 3, 3
Label1.Caption = "共有记录: " & rs.RecordCount
Set DataGrid1.DataSource = rs
End Sub 展开
2014-12-16
展开全部
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & App.Path & "\data.mdb"
你这里限制了,只有当前目录的data.mdb,所以上面也是提示找不到.mdb啊。
你这里限制了,只有当前目录的data.mdb,所以上面也是提示找不到.mdb啊。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询