是否可以从Access到QuickBooks逐行发送信息?

时间:2019-01-10 13:51:45

标签: sql vba ms-access

当前,我具有以下代码,可让我将值插入QuickBooks中的特定字段。

我试图逐行将表中的字段添加到QuickBooks中: 参见图片ex:

示例:

enter image description here

每一行的末尾都有一列,用于将条目发送给QuickBooks。如何修改我的代码以使该功能正常工作?

Public Sub exampleInsert()
Const adOpenStatic = 3
Const adLockOptimistic = 3

Dim oConnection
Dim oRecordset
Dim sMsg
Dim sConnectString
Dim sSQL

sConnectString = "DSN=Quickbooks Data;OLE DB Services=-2;"
sSQL = "Insert into customer (Name, FullName, CompanyName) values ('Testing VB', 'Full Name', 'Test Company Name')"
Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
oConnection.Open sConnectString
oConnection.Execute (sSQL)
sMsg = sMsg & "Record Added"
MsgBox sMsg


Set oRecordset = Nothing
Set oConnection = Nothing
End Sub

更新:

我添加了:

sConnectString = "DSN=Quickbooks Data;OLE DB Services=-2;"
sSQL = "Insert into customer (Name, CompanyName) Select Num, Description From TestTable"
Set oConnection = CreateObject("ADODB.Connection")
Set oRecordset = CreateObject("ADODB.Recordset")
oConnection.Open sConnectString
oConnection.Execute (sSQL)
sMsg = sMsg & "Record Added"
MsgBox sMsg

但是我收到错误消息“无效的表名:TestTable”,如何获取此SQL脚本以查看我的Access表?

1 个答案:

答案 0 :(得分:0)

要将表单的当前记录值添加到查询中,只需拉出值(例如Me.txtDescription)即可。我建议您使用ADODB.Command对象,以便可以参数化SQL并避免SQL注入:

Option Explicit

Const adOpenStatic As Integer = 3
Const adLockOptimistic As Integer = 3
Const CONNECTION_STRING As String = "DSN=Quickbooks Data;OLE DB Services=-2;"

Private Sub Command10_Click()
    Dim cn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim param As ADODB.Parameter
    Dim sMsg As String

    ' set up ADODOB connection
    Set cn = New ADODB.Connection
    cn.Open CONNECTION_STRING 

    ' set up ADODB command object
    Set cmd = New ADODB.Command
    cmd.ActiveConnection = cn

    ' note that we're using unnamed parameters,
    ' with the ? symbol
    cmd.CommandText = _
            "INSERT INTO customer " & _
            "(Name, CompanyName) " & _
            "VALUES " & _
            "(?, ?)"

    ' add form values as command parameters
    cmd.Parameters.Append cmd.CreateParameter( _
            Type:=adVarChar, _
            Size:=255, _
            value:=Me.txtNumber)

    cmd.Parameters.Append cmd.CreateParameter( _
            Type:=adVarChar, _
            Size:=255, _
            value:=Me.txtDescription)

    ' now that we have the command set up with its params,
    ' we can just execute it:
    cmd.Execute

    sMsg = "Record Added"
    MsgBox sMsg

    Set param = Nothing
    Set cmd = Nothing
    cn.Close: Set cn = Nothing
End Sub

当然,您必须使用文本框的实际名称。

另外,请注意我对原始代码进行了一些其他修改:

  • 我已定义Option Explicit。您的代码中可能已经包含此代码,但是如果没有,则需要它。这样,必须声明使用的所有变量。有关更多信息,请参见Microsoft Docs
  • 我将您的ADODB常量移到了子对象之外。理想情况下,您可以使用早期绑定并添加ADODB库引用(这样就不必自己定义它们了),或者将它们添加到单独的模块中,以便可以以任何形式使用它们。
  • 我还将您的连接字符串作为常量添加到您的子对象之外。同样,这可能应该在一个单独的模块(例如modConstants)中,您可以轻松地从项目中的任何地方引用它。
  • 我改进了代码的缩进方式。
  • 我明确添加了声明的类型(Dim sSQL as String,而不仅仅是Dim sSQL)。请注意,如果声明的变量没有类型,则默认为Variant类型,而不是String(您想要的类型)。有关更多信息,请参见Microsoft Docs