我已经设法构建一个Access VBA查询,该查询使用参数运行SQL存储过程,并且似乎运行并完成而不会发生,但是它不会将数据写入SQL数据库。我错过了编码的缩减部分吗?
从表格收集数据。
Function AddStaff()
Dim adoCN As New ADODB.Connection
Dim sConnString As String
Dim cmdObjCMD As New ADODB.Command
Dim BrandDataTxt
Dim StaffNameDataTxt
Dim NTloginIDDataTxt
Dim NMCUsernameDataTxt
Dim StaffAuditLevelDataTxt
Dim EmailAddressDataTxt
Dim PhoneLoginDataTxt
Dim TeamNameDataTxt
Dim TeamSegmentDataTxt
Dim StartDateDataTxt
Dim JobTitleDataTxt
DoCmd.OpenForm "AddNewStaff"
Forms!AddNewStaff.TBoxBrand.Value = BrandDataTxt
Forms!AddNewStaff.TBoxStaffName.Value = StaffNameDataTxt
Forms!AddNewStaff.TBoxPCLoginID.Value = NTloginIDDataTxt
Forms!AddNewStaff.TBoxNMCUsername.Value = NMCUsernameDataTxt
Forms!AddNewStaff.TBoxStaffAuditLevel.Value = CVar(StaffAuditLevelDataTxt)
Forms!AddNewStaff.TBoxEmailAddress.Value = EmailAddressDataTxt
Forms!AddNewStaff.TBoxPhoneLogin.Value = PhoneLoginDataTxt
Forms!AddNewStaff.TBoxTeamName.Value = TeamNameDataTxt
Forms!AddNewStaff.TBoxTeamSegment.Value = TeamSegmentDataTxt
Forms!AddNewStaff.TBoxStartDate.Value = CDate(StartDateDataTxt)
Forms!AddNewStaff.TBoxJobTitle.Value = JobTitleDataTxt
Set adoCN = New ADODB.Connection
sConnString = "Provider = SQLOLEDB; " & _
"Data Source = KCOMSQL26; " & _
"Initial Catalog = EclipseDW; " & _
"User ID = EclipseDW; " & _
"Password = M1Reporting; " & _
"Trusted_Connection = Yes; "
adoCN.Open sConnString
With cmdObjCMD
.ActiveConnection = sConnString
.CommandType = adCmdStoredProc
.CommandTimeout = 180
.CommandText = "Staff.usp_AddNewStaffMember"
.NamedParameters = True
.Parameters("@Brand") = " '" & BrandDataTxt & "'"
.Parameters("@StaffPrefName") = " '" & StaffNameDataTxt & "'"
.Parameters("@NTLoginID") = " '" & NTloginIDDataTxt & "'"
.Parameters("@NMCUsername") = " '" & NMCUsernameDataTxt & "'"
.Parameters("@StaffAuditLevel") = " " & StaffAuditLevelDataTxt & ""
.Parameters("@EmailAddress") = " '" & EmailAddressDataTxt & "'"
.Parameters("@PhoneLogin") = " '" & PhoneLoginDataTxt & "'"
.Parameters("@TeamName") = " '" & TeamNameDataTxt & "'"
.Parameters("@TeamSegment") = " '" & TeamSegmentDataTxt & "'"
.Parameters("@StartDate") = " " & StartDateDataTxt & ""
.Parameters("@JobTitle") = " '" & JobTitleDataTxt & "'"
.Execute
End With
adoCN.Close
End Function
SQL代码
EXEC staff.usp_AddNewStaffMember
@Brand = 'CSO North'
, @StaffPrefName = 'Test Agent'
, @NTLoginID = 'TestAgent'
, @NMCUsername = 'TestAgent'
, @StaffAuditLevel = '1'
, @EmailAddress = 'TestAgent@kcom.com'
, @PhoneLogin = '99999'
, @TeamName = 'Customer Services'
, @TeamSegment = 'Customer Services'
, @StartDate = '2017-04-18'
, @JobTitle = 'Test Agent'