J
jgd12345
Guest
Hi, I've just picked up vba to my horror when I realized that the specification was to open any forms using macros. I haven't ever used macros before but I can see that I have opened the form using the DoCmd.OpenForm command.
If anyone could show me how I could open the form with the correct filter using macros I'd be very greatful. Thanks
Here's my code:
If anyone could show me how I could open the form with the correct filter using macros I'd be very greatful. Thanks
Here's my code:
Code:
Private Sub cmdFilter_Click()
Dim stDocName As String
Dim stLinkCriteria As String
Dim Result As Integer
Dim rstCheck As ADODB.Recordset
Dim strSQL As String
Rem Declaring the formname
stDocName = "frmHouses"
Rem Filtering Minimum Price
If IsNumeric(txtMinimumPrice) = True Then
stLinkCriteria = "[H_PRICE] >= " & Me![txtMinimumPrice]
Else
Rem Added to avoid a possible error
stLinkCriteria = "[H_PRICE] >= 0 "
End If
Rem Filtering Maximum Price
If IsNumeric(txtMaximumPrice) = True Then
stLinkCriteria = stLinkCriteria & " and [H_PRICE] <= " & Me![txtMaximumPrice]
End If
Rem Filtering Region
If IsNull(cboRegion) = False Then
stLinkCriteria = stLinkCriteria & " and [H_REGION] = " & "'" & Me![cboRegion] & "'"
End If
Rem Filtering number of Rooms
If IsNumeric(txtRooms) = True Then
stLinkCriteria = stLinkCriteria & " and [H_BEDS] = " & Me![txtRooms]
End If
strSQL = "SELECT * FROM HOUSES WHERE " & stLinkCriteria
Set rstCheck = New ADODB.Recordset
rstCheck.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly, adCmdText
If rstCheck.EOF Then
MsgBox ("No records found for selected criteria.")
Else
DoCmd.OpenForm stDocName, , , stLinkCriteria
End If
rstCheck.Close
Set rstCheck = Nothing
End Sub