|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi,
I use an Access-Form to make a SQL-Query in order to select certain data. The form is rather simple: 3 Comboboxes (cboOffice, cboDepartment, cboGender) and one execute button (cmdOK_Click). The form works fantastic for all data fields which are set as "TEXT". But now I want to use the same query to perform it on "NUMBER" fields too. I tried to adapt the query but nothing works! If I set "strOffice As Number" nothing works... Here is the query: Private Sub cmdOK_Click() ' Pointer to error handler On Error GoTo cmdOK_Click_err ' Declare variables Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim strOffice As String Dim strDepartment As String Dim strGender As String Dim strSQL As String ' Identify the database and assign it to the variable Set db = CurrentDb ' Check for the existence of the query, create it if not found, ' and assign it to the variable If Not QueryExists("qryStaffListQuery") Then Set qdf = db.CreateQueryDef("qryStaffListQuery") Else Set qdf = db.QueryDefs("qryStaffListQuery") End If ' Get the values from the combo boxes If IsNull(Me.cboOffice.Value) Then strOffice = " Like '*' " Else strOffice = "='" & Me.cboOffice.Value & "' " End If If IsNull(Me.cboDepartment.Value) Then strDepartment = " Like '*' " Else strDepartment = "='" & Me.cboDepartment.Value & "' " End If If IsNull(Me.cboGender.Value) Then strGender = " Like '*' " Else strGender = "='" & Me.cboGender.Value & "' " End If ' Build the SQL string strSQL = "SELECT tblStaff.* " & _ "FROM tblStaff " & _ "WHERE tblStaff.Office" & strOffice & _ "AND tblStaff.Department" & strDepartment & _ "OR tblStaff.Gender" & strGender & _ "ORDER BY tblStaff.LastName,tblStaff.FirstName;" ' Pass the SQL string to the query qdf.SQL = strSQL ' Turn off screen updating DoCmd.Echo False ' Check the state of the query and close it if it is open If Application.SysCmd(acSysCmdGetObjectState, acQuery, "qryStaffListQuery") = acObjStateOpen Then DoCmd.Close acQuery, "qryStaffListQuery" End If ' Open the query DoCmd.OpenQuery "qryStaffListQuery" cmdOK_Click_exit: ' Turn on screen updating DoCmd.Echo True ' Clear the object variables Set qdf = Nothing Set db = Nothing Exit Sub cmdOK_Click_err: ' Handle errors MsgBox "An unexpected error has occurred." & _ vbCrLf & "Please note of the following details:" & _ vbCrLf & "Error Number: " & Err.Number & _ vbCrLf & "Description: " & Err.Description _ , vbCritical, "Error" Resume cmdOK_Click_exit End Sub |
|
#2
|
||||
|
||||
|
For textfield criteria you should add single quote before and after the value but for numbers you should not.
Sample usage For Text "Where FirstName = '" & me!txtEnteredname & "'" For Number field "Where AccountNo = " & me!txtEnteredNo For Date Field "Where BirthDate = #" & me!txtEnteredDate & "#"
__________________
V.Subramanian |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft Access Help > SQL Query in Form does work for text (string) not for numbers |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|