Versie 2+ van het Salesforce ODBC-stuurprogramma stelt u in staat om meerdere SOQL Insert-statements te batchen. Deze blog laat u zien hoe u meerdere Microsoft Access-records in Salesforce invoegt.
Om te beginnen:
- Installeer en licentieer het Salesforce.com ODBC-stuurprogramma op de computer waarop Microsoft Access is geïnstalleerd.
Voordat u het ODBC-stuurprogramma van Salesforce.com kunt gebruiken om uw toepassing te verbinden met Salesforce.com, moet u een ODBC-gegevensbron configureren. Een ODBC-gegevensbron slaat de verbindingsdetails op voor de doeldatabase (bijv. Salesforce.com) en het ODBC-stuurprogramma dat nodig is om er verbinding mee te maken (bijv. het Salesforce.com ODBC-stuurprogramma).
Om ODBC Administrator uit te voeren (die u gebruikt om een gegevensbron te maken), typt u in het dialoogvenster Uitvoeren van Windows deze opdracht als u een 64-bits versie van Microsoft Office gebruikt:
%windir%\system32\odbcad32.exe
–Of–
Typ deze opdracht als u een 32-bits versie van Microsoft Office gebruikt:
%windir%\syswow64\odbcad32.exe
Als u niet zeker weet of uw versie van Microsoft Office 32-bits of 64-bits is, start u een Office-toepassing, b.v. Microsoft Access en zoek vervolgens naar het proces van de toepassing in Taakbeheer. Als de procesnaam (voor Microsoft Access) MSACCESS.EXE *32 is, is Microsoft Office 32-bits. Als de procesnaam MSACCESS.EXE is, is Microsoft Office 64-bit.
Een Salesforce.com ODBC Driver-gegevensbron maken:
- Kies in ODBC-beheerder het tabblad Systeem-DSN en kies vervolgens Toevoegen.
- Kies in het dialoogvenster Nieuwe gegevensbron maken de optie Easysoft Salesforce ODBC SOQL-stuurprogramma en kies vervolgens Voltooien.
- Vul het dialoogvenster Easysoft Salesforce SOQL ODBC Driver DSN Setup in:
Instelling Waarde DSN SFSOQL Gebruikersnaam De naam van uw Salesforce.com-gebruiker. Bijvoorbeeld mijngebruiker@mijndomein.com. Wachtwoord Het wachtwoord voor uw Salesforce.com-gebruiker. Token Het beveiligingstoken voor uw Salesforce.com-gebruiker, indien nodig. Als u wilt weten of u een beveiligingstoken moet aanleveren, kiest u de knop Test. Als de verbindingspoging mislukt met een fout die
LOGIN_MUST_USE_SECURITY_TOKENbevat , je moet er een aanleveren.Salesforce.com e-mailt het beveiligingstoken naar het e-mailadres dat is gekoppeld aan uw Salesforce.com-gebruikersaccount. Als u geen beveiligingstoken hebt ontvangen, kunt u deze opnieuw genereren. Salesforce.com zal vervolgens het nieuwe beveiligingstoken naar u e-mailen. Om uw beveiligingstoken opnieuw te genereren, logt u in op Salesforce.com en kiest u vervolgens Set-up in het gebruikersmenu. Zoek naar "beveiligingstoken" in het vak Snel zoeken. Klik op Beveiligingstoken opnieuw instellen op de pagina Beveiligingstoken opnieuw instellen. Wanneer u de token in uw e-mailclient ontvangt, kopieert u deze en plakt u deze in het veld Token.
- Gebruik de knop Test om te controleren of u verbinding kunt maken met Salesforce.com.
Microsoft-toegang
- Maak een nieuwe Microsoft Access-database.
- Maak een tabel met de naam Account met deze kolommen:
Kolom Gegevenstype ID AutoNummering AccName Korte tekst Beschrijving van het pand Korte tekst Adres Korte tekst Stad Korte tekst Postcode Korte tekst - Voer enkele voorbeeldgegevens in de tabel in. Bijvoorbeeld:
AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
- Druk op ALT+F11 om de Visual Basic Editor te starten.
- Voeg een nieuwe module in en voeg de volgende code toe. Er zijn twee subroutines en een helperfunctie. Beide subroutines voegen de Access-records bulksgewijs in Salesforce in. De tweede subroutine laat zien hoe een geparametriseerd SOQL-insert statement gebruikt kan worden.
- Gebruik in het menu Uitvoeren Sub/UserForm uitvoeren om de subroutines uit te voeren.
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 i As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim BlockCount As String
Dim isPosted As Boolean
RowCount = 0
' Open the connection to the Easysoft Salesforce SOQL ODBC Driver data source
con.Open "SFSOQL"
comm.ActiveConnection = con
' Set up the initial insert statement using ? for each column I am going to pass in
comm.CommandText = "insert into Account (Name, BillingStreet, BillingCity, BillingPostalCode, Description ) values ( ?, ?, ?, ?, ? )"
' Bind all the columns to the statement
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
' Create a connection to the local database and start working through the rows
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
' Start a new transaction
con.BeginTrans
isPosted = False
End If
BlockCount = BlockCount + 1
Debug.Print RowCount & " : " & rs.Fields("AccName")
DoEvents
' Prepare to transfer the data to the ODBC driver
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
' When 200 rows have been sent to the driver, commit
If BlockCount = 200 Then
Debug.Print "Block posted"
con.CommitTrans
isPosted = True
BlockCount = 0
End If
' Loop through the block until the end is reached
rs.MoveNext
Loop
rs.Close
db.Close
' Finally, if there are any rows left to commit, send them
If Not isPosted Then con.CommitTrans
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 i As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
RowCount = 0
' Open the connection to the Easysoft Salesforce SOQL ODBC Driver data source
con.Open "SFSOQL"
SQLBase = "insert into Account (Name, BillingStreet, BillingCity, BillingPostalCode, Description ) values ( "
' Create a connection to the local database and start working through the rows
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
' Start a new transaction
con.BeginTrans
isPosted = False
End If
BlockCount = BlockCount + 1
Debug.Print RowCount & " : " & rs.Fields("AccName")
DoEvents
' Prepare to transfer the data to the ODBC driver
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
' When 200 rows have been sent to the driver then commit
If BlockCount = 200 Then
Debug.Print "Block posted"
con.CommitTrans
isPosted = True
BlockCount = 0
End If
' Loop through the block until the end is reached
rs.MoveNext
Loop
rs.Close
db.Close
' Finally, if there are any rows left to commit, send them
If Not isPosted Then con.CommitTrans
con.Close
End Sub
Function EscQuotes(inpStr As String) As String
EscQuotes = Replace(inpStr, "'", "''")
End Function