Access
 SQL >> Datenbank >  >> RDS >> Access

Microsoft Access-Datensätze per Bulk in Salesforce laden

Version 2 und höher des Salesforce ODBC-Treibers kann mehrere SOQL-Inserts bündeln, was es praktikabel macht, eine ganze Access-Tabelle in Blöcken statt zeilenweise nach Salesforce zu übertragen. Installieren und lizenzieren Sie den Treiber zuerst auf dem Rechner, auf dem Access läuft.

Erstellen der Datenquelle

Starten Sie den ODBC-Administrator über das Ausführen-Dialogfeld, passend zu Ihrer Office-Architektur – %windir%\system32\odbcad32.exe für 64-Bit-Office, %windir%\syswow64\odbcad32.exe für 32-Bit. Um zu prüfen, welches Sie haben, starten Sie Access und betrachten den Prozessnamen im Task-Manager: MSACCESS.EXE *32 ist 32-Bit, MSACCESS.EXE ist 64-Bit.

  1. Öffnen Sie die Registerkarte System-DSN und klicken Sie auf Hinzufügen.
  2. Wählen Sie Easysoft Salesforce ODBC SOQL Driver und klicken Sie auf Fertig stellen.
  3. Füllen Sie den Einrichtungsdialog aus:
    EinstellungWert
    DSNSFSOQL
    User NameIhr Salesforce-Benutzername, z. B. myuser@mydomain.com
    PasswordDas Passwort dieses Benutzers
    TokenIhr Sicherheits-Token, falls erforderlich. Klicken Sie auf Test, um das herauszufinden: Ein Fehler mit LOGIN_MUST_USE_SECURITY_TOKEN bedeutet, dass eines benötigt wird. Salesforce sendet das Token per E-Mail an Ihre Kontoadresse; ist es nie angekommen, melden Sie sich an, wählen Sie im Benutzermenü Setup, suchen Sie in „Quick Find“ nach „security token“ und verwenden Sie Reset Security Token. Fügen Sie den per E-Mail erhaltenen Wert hier ein.
  4. Klicken Sie auf Test, um die Verbindung zu bestätigen.

Vorbereiten der Access-Seite

  1. Erstellen Sie eine neue Access-Datenbank mit einer Tabelle namens Account:
    SpalteDatentyp
    IDAutoNumber
    AccNameShortText
    Property DescriptionShortText
    AddressShortText
    TownShortText
    PostCodeShortText
  2. Fügen Sie ein paar Beispieldatensätze hinzu:
    AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
  3. Drücken Sie ALT+F11, um den Visual Basic-Editor zu öffnen, fügen Sie ein Modul ein und fügen Sie den Code unten ein. Die erste Subroutine verwendet gebundene Parameter; die zweite zeigt, wie man einen parametrisierten SOQL-Insert als Text aufbaut:
    Option Compare Database Sub InsertAccounts() Dim con As New ADODB.Connection Dim comm As New ADODB.Command Dim PrmName As New ADODB.Parameter Dim PrmAddress As New ADODB.Parameter Dim PrmTown As New ADODB.Parameter Dim PrmPostCode As New ADODB.Parameter Dim PrmDescription As New ADODB.Parameter Dim RowCount As Long Dim db As DAO.Database Dim rs As DAO.Recordset Dim BlockCount As String Dim isPosted As Boolean RowCount = 0 ' Connect to the Easysoft Salesforce SOQL ODBC Driver data source con.Open "SFSOQL" comm.ActiveConnection = con ' One ? placeholder per column being passed in comm.CommandText = "insert into Account (Name, BillingStreet, BillingCity, " & _ "BillingPostalCode, Description ) values ( ?, ?, ?, ?, ? )" Set PrmName = comm.CreateParameter("P1", adVarWChar, adParamInput, 255, Null) Set PrmAddress = comm.CreateParameter("P2", adVarWChar, adParamInput, 255, Null) Set PrmTown = comm.CreateParameter("P3", adVarWChar, adParamInput, 120, Null) Set PrmPostCode = comm.CreateParameter("P4", adVarWChar, adParamInput, 60, Null) Set PrmDescription = comm.CreateParameter("P5", adLongVarWChar, adParamInput, 255, Null) comm.Parameters.Append PrmName comm.Parameters.Append PrmAddress comm.Parameters.Append PrmTown comm.Parameters.Append PrmPostCode comm.Parameters.Append PrmDescription Set db = CurrentDb Set rs = db.OpenRecordset("select * from Account order by Id") BlockCount = 0 Do While Not rs.EOF RowCount = RowCount + 1 If BlockCount = 0 Then con.BeginTrans ' start a new 200-row block isPosted = False End If BlockCount = BlockCount + 1 Debug.Print RowCount & " : " & rs.Fields("AccName") DoEvents PrmName.Value = rs.Fields("AccName") If Not IsNull(rs.Fields("Address")) Then PrmAddress.Value = Replace(rs.Fields("Address"), ",", vbCrLf) Else PrmAddress.Value = Null End If If Not IsNull(rs.Fields("Town")) Then PrmTown.Value = rs.Fields("Town") Else PrmTown.Value = Null End If If Not IsNull(rs.Fields("Town")) Then PrmPostCode.Value = rs.Fields("PostCode") Else PrmPostCode.Value = Null End If If Not IsNull(rs.Fields("Property Description")) Then PrmDescription.Value = rs.Fields("Property Description") Else PrmDescription.Value = Null End If comm.Execute If BlockCount = 200 Then ' block full, post it Debug.Print "Block posted" con.CommitTrans isPosted = True BlockCount = 0 End If rs.MoveNext Loop rs.Close db.Close If Not isPosted Then con.CommitTrans ' flush the tail con.Close End Sub Sub InsertAccountsParameterisedSOQL() Dim con As New ADODB.Connection Dim SQL As String Dim SQLBase As String Dim BlockCount As Long Dim isPosted As Boolean Dim RowCount As Long Dim db As DAO.Database Dim rs As DAO.Recordset RowCount = 0 con.Open "SFSOQL" SQLBase = "insert into Account (Name, BillingStreet, BillingCity, " & _ "BillingPostalCode, Description ) values ( " Set db = CurrentDb Set rs = db.OpenRecordset("select * from Account order by Id") BlockCount = 0 Do While Not rs.EOF RowCount = RowCount + 1 If BlockCount = 0 Then con.BeginTrans isPosted = False End If BlockCount = BlockCount + 1 Debug.Print RowCount & " : " & rs.Fields("AccName") DoEvents SQL = SQLBase If IsNull(rs.Fields("AccName")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(rs.Fields("AccName")) & "', " End If If IsNull(rs.Fields("Address")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(Replace(rs.Fields("Address"), ",", vbCrLf)) & "', " End If If Not IsNull(rs.Fields("Town")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(rs.Fields("Town")) & "', " End If If IsNull(rs.Fields("PostCode")) Then SQL = SQL & "NULL, " Else SQL = SQL & "'" & EscQuotes(rs.Fields("PostCode")) & "', " End If If IsNull(rs.Fields("Property Description")) Then SQL = SQL & "NULL) " Else SQL = SQL & "'" & EscQuotes(rs.Fields("Property Description")) & "')" End If con.Execute SQL If BlockCount = 200 Then Debug.Print "Block posted" con.CommitTrans isPosted = True BlockCount = 0 End If rs.MoveNext Loop rs.Close db.Close If Not isPosted Then con.CommitTrans con.Close End Sub Function EscQuotes(inpStr As String) As String EscQuotes = Replace(inpStr, "'", "''") End Function
  4. Führen Sie eine der Subroutinen über das Menü Run > Run Sub/UserForm aus und beobachten Sie, wie sich die Salesforce-Organisation in 200-Zeilen-Blöcken füllt.