Phiên bản 2+ của trình điều khiển Salesforce ODBC cho phép bạn kết hợp nhiều câu lệnh Chèn SOQL. Blog này hướng dẫn bạn cách chèn nhiều bản ghi Microsoft Access vào Salesforce.
Để bắt đầu:
- Cài đặt và cấp phép cho Trình điều khiển ODBC Salesforce.com trên máy đã cài đặt Microsoft Access.
Trước khi bạn có thể sử dụng Trình điều khiển ODBC Salesforce.com để kết nối ứng dụng của bạn với Salesforce.com, bạn cần định cấu hình nguồn dữ liệu ODBC. Nguồn dữ liệu ODBC lưu trữ chi tiết kết nối cho cơ sở dữ liệu đích (ví dụ:Salesforce.com) và trình điều khiển ODBC được yêu cầu để kết nối với nó (ví dụ:trình điều khiển ODBC Salesforce.com).
Để chạy Quản trị viên ODBC (mà bạn sử dụng để tạo nguồn dữ liệu), trong hộp thoại Chạy Windows, hãy nhập lệnh này nếu bạn đang sử dụng phiên bản 64-bit của Microsoft Office:
%windir%\system32\odbcad32.exe
–Hay–
Nhập lệnh này nếu bạn đang sử dụng phiên bản 32-bit của Microsoft Office:
%windir%\syswow64\odbcad32.exe
Nếu bạn không chắc phiên bản Microsoft Office của mình là 32 bit hay 64 bit, hãy khởi động ứng dụng Office, ví dụ:Microsoft Access, sau đó tìm quy trình của ứng dụng trong Trình quản lý tác vụ. Nếu tên quy trình là (dành cho Microsoft Access) MSACCESS.EXE * 32, thì Microsoft Office là 32-bit. Nếu tên quy trình là MSACCESS.EXE, thì Microsoft Office là 64-bit.
Để tạo nguồn dữ liệu Trình điều khiển ODBC Salesforce.com:
- Trong Quản trị viên ODBC, hãy chọn tab DSN Hệ thống, sau đó chọn Thêm.
- Trong hộp thoại Tạo Nguồn Dữ liệu Mới, chọn Trình điều khiển SOQL Easysoft Salesforce ODBC, sau đó chọn Kết thúc.
- Hoàn thành hộp thoại Thiết lập Easysoft Salesforce SOQL ODBC Driver DSN:
Setting Giá trị DSN SFSOQL Tên người dùng Tên của người dùng Salesforce.com của bạn. Ví dụ:myuser@mydomain.com. Mật khẩu Mật khẩu cho người dùng Salesforce.com của bạn. Mã thông báo Mã thông báo bảo mật cho người dùng Salesforce.com của bạn, nếu được yêu cầu. Để tìm hiểu xem bạn có cần cung cấp mã thông báo bảo mật hay không, hãy chọn nút Kiểm tra. Nếu nỗ lực kết nối không thành công với lỗi chứa
LOGIN_MUST_USE_SECURITY_TOKEN, bạn cần cung cấp một cái.Salesforce.com gửi mã thông báo bảo mật qua email đến địa chỉ email được liên kết với tài khoản người dùng Salesforce.com của bạn. Nếu bạn chưa nhận được mã thông báo bảo mật, bạn có thể tạo lại nó. Sau đó, Salesforce.com sẽ gửi mã thông báo bảo mật mới qua email cho bạn. Để tạo lại mã thông báo bảo mật của bạn, hãy đăng nhập vào Salesforce.com, sau đó chọn Thiết lập từ menu người dùng. Tìm kiếm "mã thông báo bảo mật" trong hộp Tìm nhanh. Nhấp vào Đặt lại Mã thông báo Bảo mật trong trang Đặt lại Mã thông báo Bảo mật. Khi bạn nhận được mã thông báo trong ứng dụng email của mình, hãy sao chép nó rồi dán vào trường Mã thông báo.
- Sử dụng nút Kiểm tra để xác minh rằng bạn có thể kết nối thành công với Salesforce.com.
Microsoft Access
- Tạo cơ sở dữ liệu Microsoft Access mới.
- Tạo một bảng được gọi là Tài khoản với các cột sau:
Column Loại dữ liệu ID Số tự động Tên miền Văn bản ngắn Mô tả tài sản Văn bản ngắn Địa chỉ Văn bản ngắn Thị trấn Văn bản ngắn Mã bưu điện Văn bản ngắn - Nhập một số dữ liệu mẫu vào bảng. Ví dụ:
AccName Property Description Address Town PostCode MyCo Head Office 1 MyStreet MyTown AB1 DEF AcmeLtd Workshop 1 MyRoad MyTown AB1 XYZ
- Nhấn ALT + F11 để khởi động Trình soạn thảo Visual Basic.
- Chèn một mô-đun mới và thêm mã sau. Có hai chương trình con và một chức năng trợ giúp. Cả hai chương trình con đều chèn hàng loạt bản ghi Access vào Salesforce. Chương trình con thứ hai chỉ ra cách sử dụng câu lệnh chèn SOQL được tham số hóa.
- Trên menu Run, sử dụng Run Sub / UserForm để chạy các chương trình con.
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