运行时错误3146:通过循环将数据插入mysql db时ODBC调用失败

时间:2017-11-22 15:20:08

标签: mysql ms-access access-vba odbc

通过这个过程,我试图将值插入服务器数据库,但它只插入一行循环并显示error 3146 odbc call failed。另一方面它工作正常,如果我连接我的本地机器如下:

blnSuccess = AttachDSNLessTable("tbl_agreement", "tbl_agreement", "127.0.0.1", "tenant_db", "******", "******")
blnSuccess = AttachDSNLessTable("tbl_agreement_years", "tbl_agreement_years", "127.0.0.1", "tenant_db", "******", "******")

不知道我错过了什么,任何帮助都会非常感激。

Private Sub cmdUpdate_Click()
    Dim t_id, ag_id, rate, incr_rate As Integer
    Dim stDate, nxstDate, nxenDate, nxyrDate, enDate As Date
    Dim exp_date As Date
    Dim StrSqL As String

If IsNull(Me.tenant_id) Or Me.tenant_id = "" Then
    MsgBox "You must select a Tenant to create new agreement", vbInformation, "Tenant"
    Me.tenant_id.SetFocus
    Me.tenant_id.Dropdown
Else
    t_id = Me.tenant_id
    ag_id = Me.id
    stDate = Me.startdate
    enDate = Me.enddate
    rate = Me.initial_rate
    incr_rate = Me.increase_rate

    nxyrDate = stDate
    Do While nxyrDate < enDate
        nxstDate = nxyrDate
        nxyrDate = DateAdd("yyyy", 1, nxstDate)
        nxenDate = DateAdd("d", -1, nxyrDate)

        'Debug.Print t_id, ag_id, nxstDate, nxenDate, rate
        StrSqL = "INSERT INTO tbl_agreement_years(tenant_id, ag_id, interval_start, interval_end, rate)" & _
            "VALUES (" & t_id & "," & ag_id & ",#" & Format(nxstDate, "mm/dd/yyyy") & "#,#" & Format(nxenDate, "mm/dd/yyyy") & "#," & rate & ")"
        CurrentDb.Execute StrSqL, dbFailOnError
        rate = rate + incr_rate
    Loop
    Me.Refresh
    MsgBox "Saved successfully", vbInformation, "Save"
End If

上传连接:

Public Function InitApplication()
Dim blnSuccess As Boolean
   "tbl_tenant_basic_info", "192.168.20.2", "tenant_db", "admin", "1DBServer")
    blnSuccess = AttachDSNLessTable("tbl_agreement", "tbl_agreement", "192.168.20.2", "tenant_db", "******", "******")
    blnSuccess = AttachDSNLessTable("tbl_agreement_years", "tbl_agreement_years", "192.168.20.2", "tenant_db", "******", "******")



    If blnSuccess Then
        DoCmd.OpenForm "frmTenant_basic_info"
    Else
        MsgBox "Boohoo, could not refresh the links to the database. Call a programmer", vbCritical
    End If
End Function

AttachDSNLessTable功能:

Function AttachDSNLessTable(stLocalTableName As String, stRemoteTableName As String, stServer As String, stDatabase As String, Optional stUsername As String, Optional stPassword As String)
On Error GoTo AttachDSNLessTable_Err
Dim td As TableDef
Dim stConnect As String

For Each td In CurrentDb.TableDefs
    If td.name = stLocalTableName Then
        CurrentDb.TableDefs.Delete stLocalTableName
    End If
Next

If Len(stUsername) = 0 Then
    '//Use trusted authentication if stUsername is not supplied.
    stConnect = "ODBC;DRIVER={MySQL ODBC 5.2 Unicode Driver};SERVER=" & stServer & ";DATABASE=" & stDatabase & ";Trusted_Connection=Yes"
Else
    '//WARNING: This will save the username and the password with the linked table information.
    stConnect = "ODBC;DRIVER={MySQL ODBC 5.2 Unicode Driver};Server=" & stServer & ";Database=" & stDatabase & ";Uid=" & stUsername & ";Pwd=" & stPassword & ";Option= 3"
End If
Set td = CurrentDb.CreateTableDef(stLocalTableName, dbAttachSavePWD, stRemoteTableName, stConnect)
CurrentDb.TableDefs.Append td
AttachDSNLessTable = True
Exit Function

AttachDSNLessTable_Err:

    AttachDSNLessTable = False
    MsgBox "AttachDSNLessTable encountered an unexpected error: " & Err.Description

End Function

0 个答案:

没有答案