我怀疑这是一个简单的语法问题,但我被卡住了。
我有这个功能:
Function CreateSPT(SPTQueryName As String, SQLString As String, _
ConnectString As String)
Dim mydatabase As Database, myquerydef As QueryDef, MyRS As Recordset
Set mydatabase = DBEngine.Workspaces(0).Databases(0)
Set myquerydef = mydatabase.CreateQueryDef(SPTQueryName)
myquerydef.Connect = ConnectString
myquerydef.SQL = SQLString
myquerydef.ReturnsRecords = False
myquerydef.ReturnsRecords = True
Set MyRS = myquerydef.OpenRecordset()
'MyRS.MoveFirst
DoCmd.OpenQuery SPTQueryName, acViewNormal, acReadOnly
myquerydef.Close End Function
当我的表格上按下一个按钮时,会调用它。
Private Sub Btn_Search_Click()
If Not IsNull(DLookup("[AccessionNumber]", "[SA2001]", "[AccessionNumber] = '" & [Forms]![SA2001]![Txt_LabEpNo] & "'")) Then
'MsgBox "Found"
'Populates the form as required
'I've removed the code as it's messy
Else
'MsgBox "Not Found"
Me.Refresh
Dim SPTQueryName As String
Dim SQLString As String
Dim ConnectString As String
DoCmd.Close acQuery, "XQuery"
DoCmd.DeleteObject acQuery, "XQuery"
SPTQueryName = "XQuery"
SQLString = "SELECT SQLUser.EP_VisitNumber.EPVIS_DebtorNumber_DR as HospitalNumber, SQLUser.EP_VisitNumber.EPVIS_GivenName as Forename, SQLUser.EP_VisitNumber.EPVIS_Surname as Surname, SQLUser.EP_VisitNumber.EPVIS_DateOfBirth as DOB, SQLUser.EP_VisitNumber.EPVIS_DoctorCode_DR->CTDR_Surname as CONS, SQLUser.EP_VisitNumber.EPVIS_VisitNumber as AccessionNumber, SQLUser.EP_VisitNumber.EPVIS_DateOfCollection as SampleReceivedDate, SQLUser.EP_VisitTestSet.VISTS_TestSet_DR->CTTS_Department_DR->CTDEP_NAME as LabDept FROM SQLUser.EP_VisitTestSet, SQLUser.EP_VisitNumber WHERE ( SQLUser.EP_VisitTestSet.VISTS_ParRef=SQLUser.EP_VisitNumber.EPVIS_VisitNumber ) AND ( SQLUser.EP_VisitTestSet.VISTS_ParRef = '1500000000')"
ConnectString = "ODBC;DSN=DSNNAME;SERVER=IPADDRESS;PORT=1972;DATABASE=DATABASENAME;AUTHENTICATION METHOD=0;UID=USERNAME;PWD=PASSWORD;STATIC CURSORS=0;QUERY TIMEOUT=0;UNICODE SQLTYPES=0"
Call CreateSPT(SPTQueryName, SQLString, ConnectString)
'Me.[Hospital Number] = rec("HospitalNumber")
Me.Refresh
Me.[Hospital Number] = Null
Me.[Hospital Number] = MyRS("HospitalNumber")
End If End Sub
我的问题是我无法使用传递查询的结果填充表单。
基本上,我如何声明记录集,以便我可以将表格填充为Me。[医院编号] = MyRS(" HospitalNumber")不起作用" MyRS"功能未定义。
答案 0 :(得分:0)
您如何理解这两个陈述应该协同工作:
myquerydef.ReturnsRecords = False
Set MyRS = myquerydef.OpenRecordset()
您必须有一个返回记录的查询,然后从中打开一个记录集,然后从记录集中提取一个值。
您在另一个函数中声明MyRs。 你必须有类似的东西:
MyRs = somequery.OpenRecordset()
Me![Hospital Number].Value = MyRs!HospitalNumber.Value