我对VBA和SQL相当新,并寻求调试指导。我试图基于user_ID从SQL服务器检索表单的txt值的数据。但是,有一个
语法错误3131尝试运行' -case" CA时FROM子句出错 数据库"'
我已经测试了excel中的连接,它在复制到Activecell时有效。
编辑:感谢您的建议,即使我将txtloginID.value缩小到一定值(Robert),我也会遇到相同的语法错误。错误始于:
StrQuery = "SELECT * FROM [CCOADMIN].[DBO].[TBLUSER_ACCESS] WHERE ACF_ID ='" & txtLoginID.Value & "'"
Set RS_Recordset = CurrentDb.OpenRecordset(StrQuery)"
Private Sub FindUser()
Dim RS_Recordset As Recordset
Dim TempQry As String
If txtLoginID.Value = "" Or IsNull(txtLoginID.Value) = True Then
MsgBox "Please enter a LoginID and try again.", vbOKOnly, "Missing Information"
Exit Sub
Else
Select Case lblCurrentTool.Caption
Case "Vacation Tracker"
TempQry = "SELECT * FROM VacUsers WHERE LoginID = '" & txtLoginID.Value & "'"
Set RS_Recordset = CurrentDb.OpenRecordset(TempQry)
If RS_Recordset.RecordCount = 0 Then GoTo NotFound
With RS_Recordset
txtFullName.Value = .Fields("FullName")
txtManLoginID.Value = .Fields("ManLoginID")
txtLocation.Value = .Fields("Location")
cmbStatus.Value = .Fields("Active")
cmbGroup.Value = .Fields("[Group]")
cmbRole.Value = .Fields("Role")
cmbJFC.Value = .Fields("JobFunctionCode")
cmbTrainingStage.Value = ""
End With
Dim RS_Recordset As Recordset
Dim TempQry As String
Select Case lblCurrentTool.Caption
Case "CA Database"
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
ConnectionString = "Provider=SQL;Password=;Persist Security Info=True;User ID=PBLPRSM0;Initial Catalog=CCOAdmin;Data Source=CRSDPSSCA0BLPRM.TDBFG.COM,3341;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=TDL05107803;Use Encryption for Data=False;Tag with column collation when possible=False"
cnn.Open ConnectionString
cnn.CommandTimeout = 900
StrQuery = "SELECT * FROM [CCOADMIN].[DBO].[TBLUSER_ACCESS] WHERE ACF_ID = '" & txtLoginID.Value & "'"
Set RS_Recordset = CurrentDb.OpenRecordset(StrQuery)
If RS_Recordset.RecordCount = 0 Then GoTo NotFound
With RS_Recordset
txtFullName.Value = .Fields("Emloyee_Name")
'txtManLoginID.Value = .Fields("ManLoginID")
'txtLocation.Value = .Fields("Location")
'cmbStatus.Value = .Fields("Active")
'cmbGroup.Value = .Fields("BusGroup")
'cmbRole.Value = .Fields("CurrentLimit")
'cmbJFC.Value = .Fields("JobFunctionCode")
'cmbTrainingStage.Value = ""
End With
End Select
End If
RS_Recordset.Close
Set RS_Recordset = Nothing
Exit Sub
NotFound:
MsgBox "The User was not found in this database. Please check that the LoginID entered is correct and try again.", vbOKOnly, "User Not Found"
RS_Recordset.Close
Set RS_Recordset = Nothing
End Sub