如何将ADO记录集保存到Access 2003中的新本地表?

时间:2010-07-22 05:56:41

标签: ms-access import save recordset

我正在尝试将表从FoxPro 9.0数据库导入Access 2003.到目前为止,从Google搜索和许多试验中,我连接到表的唯一方法是以编程方式通过OLE DB连接。我已经设置了3个具有不同配置的ODBC连接,但它们都不起作用:我得到“未指定的错误”,我找不到任何信息。

使用OLE DB,我可以成功连接到FoxPro数据库,并导入ADO记录集中的表。问题是我无法使用SQL将记录集保存到本地数据库中的新表中。 ADO记录集的行为与表不同,因此我无法查询它们。下面的代码在DoCmd.RunCommand ("select * from " & rst & " INTO newClients")处给出了“类型不匹配”错误。

Sub newAdoConn()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim strSQL As String
Dim decision As Integer

Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=vfpoledb;" & _
       "Data Source=s:\jobinfo\data\jobinfo.dbc;" & _
       "Mode=ReadWrite|Share Deny None;" & _
       "Collating Sequence=MACHINE;" & _
       "Password=''"
strSQL = "Select * from Jobs"
cnn.Open
Set rst = cnn.Execute("Select * from clients")

If rst.EOF = False Then
    Do While Not rst.EOF
        decision = MsgBox(rst.Fields!ID & " " & rst.Fields!fname & " " & rst.Fields!lname & vbCrLf & vbCrLf & "Continue?", vbYesNo)
        If decision = vbYes Then
            rst.MoveNext
        Else
            Exit Do
        End If
    Loop
End If

DoCmd.RunCommand ("select * from " & rst & " INTO newClients")
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing

End Sub

4 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

您需要做的是将FoxPro表作为记录集打开,并将本地表作为另一个记录集打开。然后,您可以遍历FoxPro记录集并执行类似这样的操作

Do until FoxProRst.EOF
   LocatRst.AddNew
      LocalRst!SomeField1=FoxProRst!SomeField1
      LocalRst!SomeField2=FoxProRst!SomeField2
      LocalRst!SomeField3=FoxProRst!SomeField3
   LocalRst.Update
   FoxProRst.MoveNext
Loop

它可能不是最快捷的方式,但它会起作用

答案 2 :(得分:0)

让我简单介绍一下SQL查询的另一种方法,可以简化:

'...
'not required for first time test:
'cnn.Execute("DROP TABLE MyNewTable")
'...
'create the new table in the destination Access database
cnn.Execute("CREATE TABLE MyNewTable (MyField1 int, MyField2 VARCHAR(20), MyField3 Int)")

'insert data in the new table from a select query to the original table
Dim sSQL as string, MyOriginalDBPath as String
sSQL = "INSERT INTO MyNewTable (MyField1, MyField2, MyField3) SELECT OriginalField1, OriginalField2, OriginalField3 FROM [" & MyOriginalDBPath & ";PWD=123].clients"

Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset

rs.Open sSQL, cnn, adOpenForwardOnly, adLockReadOnly, adCmdText
'...

注意:这个'草案'想法假设连接字符串是对Access数据库进行的,并且与原始数据库的连接将在SQL字符串内,但我不知道正确的sintaxis。我只用不同的访问数据库测试了这种方法。 请注意,这适用于Access:... [" &安培; MyOriginalDBPath& &#34 ;; PWD = 123] ...

  

The Jet database engine can reference external databases in SQL statements by using a special syntax有三种不同的格式:

     

[Microsoft Access数据库的完整路径]。[表名]

     

[ISAM名称; ISAM连接字符串]。[表名]

     

[ODBC; ODBC连接字符串]。[表名]

     

...

     

您可以使用ODBC数据源名称(DSN)或无DSN连接字符串:

     

DSN:[odbc; DSN =; UID =; PWD =]

     

DSN-less:[odbc; Driver = {SQL Server}; Server =; Database =;          UID =; PWD =]

一些参考文献:

Querying data by joining two tables in two database on different servers

C# - Join tables from two different databases using different ODBC drivers

答案 3 :(得分:-1)

为什么不使用ODBC链接到表? http://support.microsoft.com/kb/225861