data type mismatch on criteria expression - ms-access-2007

I have three tables with the exact same data fields and data types. I am trying to create a search form using one combo box for three subforms. I tried changing the String to Integer but I always end up with the same error: "Data type mismatch on criteria expression". What am I doing wrong here.
Private Sub cboAlarmSearch_AfterUpdate()
Dim SpotAlarm As String
Dim ProgressAlarm As String
Dim FinalAlarm As String
Dim strAlarmCriteria As String
If IsNull(Me.cboAlarmSearch) Or Me.cboAlarmSearch = "" Then
MsgBox "Please choose an Alarm to search.", vbOKOnly, "Alarm Required"
Else
strAlarmCriteria = Me.cboAlarmSearch.Value
'Alarm search for Spot
SpotAlarm = "Select * FROM tblSpot WHERE ([Alarm] = '" & strAlarmCriteria & "')"
Me.frmSpotSearchSubform.Form.RecordSource = SpotAlarm
Me.frmSpotSearchSubform.Form.Requery
'Alarm search for Progress
ProgressAlarm = "Select * from tblProgress WHERE ([Alarm] = '" & strAlarmCriteria & "')"
Me.frmProgressSearchSubform.Form.RecordSource = ProgressAlarm
Me.frmProgressSearchSubform.Form.Requery
'Alarm search for Final
FinalAlarm = "Select * from tblFinal WHERE ([Alarm] = '" & strAlarmCriteria & "')"
Me.frmFinalSearchSubform.Form.RecordSource = FinalAlarm
Me.frmFinalSearchSubform.Form.Requery
End If
End Sub

Related

Use bookmark value in WORD for sql query

I am currently trying to build an automated WORD business letter that fills out corresponding data such as company address, company name, today's date, deadline date and so on after I have typed in a 5 digit number.
The number and all other fields are bookmarks.
Whenever the user types in a specific 5 digit number into the bookmark named "theNumber", this value will be used for an SQL query (Oracle). The query then fills out all other bookmarks!
Here is what I have tried so far:
Function dbQuery(ByVal TM As String, ByVal myNumber As String) As Boolean
Dim sqlrumpf As String
Dim sqlstring As String
Dim connstring As String
With ActiveDocument
If .Bookmarks.Exists(TM) Then
Dim TMRange As Range
Set TMRange = .Bookmarks(TM).Range
TMRange = myNumber
.Bookmarks.Add TM, TMRange
dbQuery = True
Else
Debug.Print "Bookmark not found: " & TM
End If
End With
sqlrumpf = "SELECT p.name " & _
"FROM xy.person p, xy.adresse a, wz.zusatz1 z, xy.vorgang v " & _
"WHERE p.adresse_id = a.adresse_id " & _
"AND z.aktnr = v.vorgang_id " & _
"AND a.adresse_id = v.adresse_id " & _
"AND v.vorgang_nr = '"
sqlstring = sqlrumpf & myNumber & "'"
connstring = "ODBC;DSN=mydsn;UID=myuid;DBQ=my.dbq.lan;DBA=W;APA=T;PFC=1;TLO=0;PWD=mypw;"
QueryTables.Add _
(Connection:=connstring, _
Destination:=Range(TMRange), _
Sql:=sqlstring).Refresh
End Function
Sub Main()
If dbQuery("theNumber", "12345") = False Then
MsgBox "Database query failed!"
End If
End Sub
I get the error:
Runtime Error '4608': Value not within definition range.
How can I fix this?
The error seems to occur here:
Destination:=Range(TMRange), _
Normally I use this query only for excel when I want to print a query into a single cell.
Is it generally a bad idea to use bookmarks with sql queries in this manner?
Which fields would you use if not bookmarks?
How do you query bookmark values?

SQL query works in access but not in excel

I need to get data from a recordset. The SQL query works fine in MS Access and returns exactly the expected values, but when the same query is lunched in VBA Excel, I get the following error:
No value given for one or more required parameters
Do you have any ideas why this problem occurs?
Thank you.
Philippe-Olivier Roussel
Private Sub CBtype_AfterUpdate()
Dim strConnexion As String
Dim connexion As New ADODB.Connection
strConnexion = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Database & ""
connexion.Open strConnexion
Dim rsMarque As New ADODB.Recordset
Dim seltype As String
seltype = CBtype.Value
rsMarque.Open "SELECT DISTINCT tblMarque.marque_nom FROM tblMarque, tblModele WHERE " & _
" tblMarque.marque_id = tblModele.marque_id AND tblModele.marque_id IN " & _
" (SELECT DISTINCT tblModele.marque_id FROM tblModele, tblType " & _
" WHERE tblModele.type_id = tblType.type_id AND tblModele.type_id = " & _
" (SELECT tblType.type_id FROM tblType WHERE " & _
" (tblType.type_nom = " & seltype & ")))", connexion, adOpenStatic
rsMarque.MoveFirst
With UserForm2.CBmarque
.Clear
Do
.AddItem rsMarque!marque_nom
rsMarque.MoveNext
Loop Until rsMarque.EOF
End With
End Sub
This error message looks like an output from the DBMS rather than Excel
I think you might be missing the apostrophe before and after your string variable. See if
" (tblType.type_nom = '" & seltype & "')))" works (I'm assuming the column you're querying is varchar type, since you declared seltype as string)

visual basic like error

Private Sub txtmid_Change()
On Error Resume Next
Mmid = txtmid.Text
Adodc1.RecordSource = "select * from members where txtmid like '" & Mmid & "'"
Adodc1.Refresh
Mname = Adodc1.Recordset.Fields("Mname").Value
Expiryd = Adodc1.Recordset.Fields("Expiryd").Value
txtname.Text = Mname
txtedate(1).Text = Format(Expiryd, "dd / mm / yyyy")
End Sub
I am getting FROM clause error. Please help me to resolve this error. Thank you.
Try this:
First of all, remove On Error Resume Next, because it's dangerous (you should use a very error handler, instead).
Adodc1.RecordSource = "select * from members where Mmid = '" & Mmid & "'"
Note: anyway, for fields of TEXT type, you should use always Replace$() to 'double quote' to avoid mistake when string values contains a single quote. Example:
Dim sql As String
Dim sSearch As String
sSearch = "You are 'magic' developer"
sql = "SELECT * FROM Users WHERE Note = '" & Replace$(sSearch, "'", "''") & "'"
Otherwise, in this case, if you use (wrongly):
sql = "SELECT * FROM Users WHERE Note = '" & sSearch & "'"
You will get a error.

Incorrect syntax near 's'. Unclosed quotation mark after the character string

I'm using a query to pull data from an SQL database, at times the last dropdown im using to get the record i'm looking for has a single quote, when it does I get the following error: Incorrect syntax near 's'. Unclosed quotation mark after the character string
This is the code I have:
Using objcommand As New SqlCommand("", G3SqlConnection)
Dim DS01 As String = DDLDS01.SelectedItem.Text
Dim State As String = DDLState.SelectedItem.Text
Dim Council As String = DDLCouncil.SelectedItem.Text
Dim Local As String = DDLLocal.SelectedItem.Text
Dim objParam As SqlParameter
Dim objDataReader As SqlDataReader
Dim strSelect As String = "SELECT * " & _
"FROM ConstitutionsDAT " & _
"WHERE DS01 = '" & DS01 & "' AND STATE = '" & State & "' AND COUNCIL = '" & Council & "' AND LOCAL = '" & Local & "' AND JURISDICTION = '" & DDLJurisdiction.SelectedItem.Text & "' "
strSelect.ToString.Replace("'", "''")
objcommand.CommandType = CommandType.Text
objcommand.CommandText = strSelect
Try
objDataReader = objcommand.ExecuteReader
DDLJurisdiction.Items.Add("")
While objDataReader.Read()
If Not IsDBNull(objDataReader("SUBUNIT")) Then
txtSubUnit.Text = (objDataReader("SUBUNIT"))
End If
If Not IsDBNull(objDataReader("DS02")) Then
lblDS02.Text = (objDataReader("DS02"))
End If
If Not IsDBNull(objDataReader("LEGISLATIVE_DISTRICT")) Then
txtALD.Text = (objDataReader("LEGISLATIVE_DISTRICT"))
End If
If Not IsDBNull(objDataReader("REGION")) Then
txtRegion.Text = (objDataReader("REGION"))
End If
If DDLState.SelectedItem.Text <> "OTHER" Then
If Not IsDBNull(objDataReader("UNIT_CODE")) Then
txtUnitCode.Text = (objDataReader("UNIT_CODE"))
End If
End If
End While
objDataReader.Close()
Catch objError As Exception
OutError.Text = "Error: " & objError.Message & objError.Source
Exit Sub
End Try
End Using
Not all records contain a single quote, only some, so i'd need something that would work if a single quote is present or not.
Thanks.
Your problem is this line here:
strSelect.ToString.Replace("'", "''")
This is changing your WHERE clause from something like
WHERE DS01 = 'asdf' AND ...
To:
WHERE DS01 = ''asdf'' AND ...
You need to do the replace on the individual values in the where clause, not on the whole select statement.
What you should really be doing is using a parameterized query instead.
Update: added same link as aquinas because it's a good link
Use parameterized queries, and only EVER use parameterized queries. See: How do I create a parameterized SQL query? Why Should I?

Update with NULL for a Numeric field with a textfield in a form

I want to be able to update a numeric column to different values and also be able clear values in a field (set the field to null). I am able to update the field to any number I want but whenever I delete the value and set it to nothing, it's giving me an error.
Here's my code:
Private Sub txtPortNumber_AfterUpdate()
Dim portSQL As String
portSQL = "UPDATE Switches SET [Port Number] = " & Me.txtPortNumber & " WHERE ID = " & Me.txtID3 & ""
DoCmd.RunSQL (portSQL)
Me.Refresh
End Sub
How would you recommend me approaching this issue? I'm thinking that I need to write an IF-ELSE statement but I'm unsure of what to put.
txtID3 is a number and txtPortNumber is also numeric.
Sorry I got started on this, I thought it was VB.NET and not Access. Let me know if this works:
Private Sub txtPortNumber_AfterUpdate()
Dim portSQL As String
Dim portNumber as Integer
Dim myID as Integer
If IsNumeric(Me.txtID3) Then
myID = CInt(Me.txtID3)
If IsNumeric(Me.txtPortNumber) Then
portNumber = CInt(Me.txtPortNumber)
portSQL = "UPDATE Switches SET [Port Number] = " & portNumber & " WHERE ID = " & myID & ""
Else
portSQL = "UPDATE Switches SET [Port Number] = NULL WHERE ID = " & myID & ""
End If
DoCmd.RunSQL (portSQL)
End If
Me.Refresh
End Sub