【问题标题】:MS Access Insert results into table from sql serverMS Access 将结果从 sql server 插入到表中
【发布时间】:2016-11-03 22:21:36
【问题描述】:

我尝试使用以下代码从 sql 中取回数据。连接很好,一切正常,但是,我想将以下代码的内容存储在访问表中。 请问有人可以帮忙吗?

 Public PERSONALDBCONT As Object, _
 SQLSTR As String, SQLSTR1 As String, _
 SQLSTR2 As String, SQLSTR3 As String, _
 RecCount As String, DB As String
Function CONNECT_TO_DB()
    Set PERSONALDBCONT = CreateObject("ADODB.connection")
    Dim SCONN As String
SCONN = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=" & tempdb & ";Data Source=WBACUKSQLPD001;" & _
"Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=WBAC173427;Use Encryption for Data=False;Tag with column collation when possible=False"
        PERSONALDBCONT.Open SCONN


End Function



Function CLOSE_CONNECTION_TO_SQL()

    On Error Resume Next

        PERSONALDBCONT.Close

        Set PERSONALDBCONT = Nothing

    On Error GoTo 0

End Function

Sub SQL_()

            Dim rs As Object

            Dim iCols As Integer

            Set rs = CreateObject("ADODB.Recordset")

            On Error GoTo ERR



                CONNECT_TO_DB
          Dim SQLSTR As String
            SQLSTR = "Select top 1 * from sys.objects"
                rs.Open SQLSTR, PERSONALDBCONT




                Exit Sub



ERR:

            CLOSE_CONNECTION_TO_SQL

            MsgBox "There was an error at " & Stage & "." & vbNewLine & "Please see the instructions and investigate"

            If Application.Visible = False Then Application.Visible = True

            End

End Sub

【问题讨论】:

  • 能不能链接到access DB,然后简单的INSERTINTO MAKE TABLE等
  • 嗨@Nathan_Sav,我不确定你所说的链接是什么意思。
  • google 将 SQL 服务器表链接到 Access。
  • 啊,对不起。我以为你的意思类似于excel的连接。这没有用,因为我需要运行查询并且我没有对 sql 的写入权限,因此无法创建 proc,它不像从表中选择那么简单。
  • 只在访问中使用链接表,或者获得访问权限来完成它

标签: sql vba ms-access insert


【解决方案1】:

对于不与完整表交互的特定查询,请考虑预先构建可容纳来自 SQL Server 查询的数据的 Access 表。然后,使用迭代追加查询或记录集更新将 SQL Server 记录集迁移到 Access 表:

追加查询 (使用参数化查询定义)

' ... same code as above ... '
Dim db As Database
Dim qdef As Querydef
Dim strSQL As String

Set db = CurrentDb

' PREPARE SQL STATEMENT (SPECIFY PARAM NAMES AND TYPES IN FIRST LINE) '
strSQL = "PARAMETERS Col1 Text(100), Col2 Text(100), Col3 Integer, Col4 Boolean;" _
          & " INSERT INTO AccessDestinationTable (Col1, Col2, Col3)" _
          & " VALUES ([Col1], [Col2], [Col3]);"

' LOOP THROUGH SQL SERVER RS '
Do While Not SqlServerRS.EOF

   Set qdef = db.CreateQueryDef("", strSQL)
   qdef!Col1 = SqlServerRS!Col1
   qdef!Col2 = SqlServerRS!Col2
   qdef!Col3 = SqlServerRS!Col3

   qdef.Execute

   SqlServerRS.MoveNext
Loop

SqlServerRS.Close

Set qdef = Nothing
Set db = NOthing

记录集更新 (使用两个打开的记录集)

' ... same code as above ... '
Dim db As Database
Dim AccessRS As Recordset

Set db = CurrentDb
Set AccessRS = db.OpenRecordset("AccessDestinationTable")

' LOOP THROUGH SQL SERVER RS '
Do While Not SqlServerRS.EOF
    ' OPEN EDIT MODE OF ACCESS RECORSET, SET VALUES TO FIELDS, APPEND ROW '
    With AccessRS
       .Edit 

       !Col1 = SqlServerRS!Col1
       !Col2 = SqlServerRS!Col2
       !Col3 = SqlServerRS!Col3

       .Update 
    End With

    SqlServerRS.MoveNext
Loop

AccessRS.Close
SqlServerRS.Close

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多