MS Access - 多个搜索表单

时间:2018-05-08 15:33:18

标签: vba ms-access

enter image description here

我需要您协助在MS Access中为多个搜索字段创建表单。我的表单有10个字段,用户可以在其中输入数据以获得拆分表单中的搜索结果。预期用户可以选择在一个或多个字段中输入搜索参数以获得结果(例如:供应商,州,许可证号,许可证到期日期的日期参数,添加日期等)。我目前在使用VB时遇到了麻烦,并打了一堵砖墙。

如果没有使用任何搜索字段,我希望显示所有结果。

如果在任何搜索字段中都有搜索条件,我希望结果显示搜索字段中的任何条件(例如:搜索供应商'亚马逊'并按状态'FL'搜索。我想要结果返回亚马逊表格中的所有记录,并将FL记录为状态而不是亚马逊和FL记录,但表格中包含亚马逊或FL的记录。

以下是我的VB编码:

Sub Search()

Dim tempCriteriaForReport As TempVar

Dim strSearch, strCriteria, strLicenseType, SearchDateRange As String

Dim strVendor, strState, strDrugManufacturer, strDeviceManufacturer, strFederalDrugIdentifier, strCommentsFKA, strLicenseNumber, strExpirationDate, strDateAdded As String

Dim task As String

Dim varItem As Variant


'''=========== code for VENDOR
If Not IsNull(Me.txtVENDOR) Then
    strVendor = "([Vendor] like ""*" & Me.txtVENDOR & "*"")"
    strCriteria = strCriteria & " And (" & strVendor & ")"
End If

'''=========== code for STATE
If Not IsNull(Me.txtSTATE) Then
    strState = "([State] like ""*" & Me.txtSTATE & "*"")"
    strCriteria = strCriteria & " And (" & strState & ")"
End If

'''=========== code for DRUG MANFACTURER
If Not IsNull(Me.txtDRUGMANUFACTURER) Then
    strDrugManufacturer = "([Is Vendor a Drug Manufacturer] like ""*" & Me.txtDRUGMANUFACTURER & "*"")"
    strCriteria = strCriteria & " And (" & strDrugManufacturer & ")"
End If

'''=========== code for DEVICE MANFACTURER
If Not IsNull(Me.txtDEVICEMANUFACTURER) Then
    strDeviceManufacturer = "([Is Vendor a Device Manufacturer] like ""*" & Me.txtDEVICEMANUFACTURER & "*"")"
    strCriteria = strCriteria & " And (" & strDeviceManufacturer & ")"
End If

'''=========== code for FEDERAL DRUG IDENTIFIER
If Not IsNull(Me.txtFEDERALDRUGIDENTIFIER) Then
    strFederalDrugIdentifier = "([Federal Drug Facility Establishment Identifier] like ""*" & Me.txtFEDERALDRUGIDENTIFIER & "*"")"
    strCriteria = strCriteria & " And (" & strFederalDrugIdentifier & ")"
End If

'''=========== code for LICENSE NUMBER
If Not IsNull(Me.txtLICENSENUMBER) Then
    strLicenseNumber = "([License Number] like ""*" & Me.txtLICENSENUMBER & "*"")"
    strCriteria = strCriteria & " And (" & strLicenseNumber & ")"
End If

'''=========== code for LICENSE TYPE
If Not IsNull(Me.txtLICENSETYPE) Then
    strLicenseNumber = "([License Type] like ""*" & Me.txtLICENSETYPE & "*"")"
    strCriteria = strCriteria & " And (" & strLicenseType & ")"
End If

'''=========== code for LICENSE EXPIRATION DATE range
If Not IsNull(Me.txtEXPIRATIONDATEFROM) And Not IsNull(Me.txtEXPIRATIONDATETO) Then
    SearchDateRange = "([Expiration Date] >= #" & Me.txtEXPIRATIONDATEFROM & "# and [Expiration Date] <= #" & Me.txtEXPIRATIONDATETO & "#)"
    strCriteria = strCriteria & " And (" & SearchDateRange & ")"
End If

'''=========== code for DATE ADDED range
If Not IsNull(Me.txtDATEADDEDFROM) And Not IsNull(Me.txtDATEADDEDTO) Then
    SearchDateRange = "([Date Added] >= #" & Me.txtDATEADDEDFROM & "# and [Date Added] <= #" & Me.txtDATEADDEDTO & "#)"
    strCriteria = strCriteria & " And (" & SearchDateRange & ")"
End If


End Sub

0 个答案:

没有答案