0

With this procedure I'm trying to insert value into a server database but it insert only one row with the loop and show error 3146 odbc call failed. On the other hand it works fine, if I connected this my local machine like below:

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", "******", "******")

Don't know what I missed actually, any help would much appreciated.

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

Onload Connection:

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:

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
Mir Abzal Ali
  • 569
  • 5
  • 9
  • 27
  • see https://stackoverflow.com/a/26224418/3820271 to find the root cause of the error. – Andre Nov 22 '17 at 16:06
  • or https://stackoverflow.com/questions/730414/determine-real-cause-of-odbc-failure-error-3146-with-ms-access – Andre Nov 22 '17 at 16:08
  • If you want help troubleshooting a specific query, please provide that query (the query causing the error). See [how to debug dynamic sql](https://stackoverflow.com/questions/418960/managing-and-debugging-sql-queries-in-ms-access/1099570#1099570) – Erik A Nov 22 '17 at 16:32

0 Answers0