0
Good afternoon!
I have a problem with a query in Access via form.
The Form is from Excel vba, which connects to the database.
Below is the complete code.
This is the code of the button. Clicking this validates which of the two buttons is marked (true value). If neither is selected it shows error message.
If BP option is checked, it calls the function and passes to it the BP number typed by the user in the input box. The same happens if the CPF is selected.
Private Sub btn_consulta_Click()
Dim BP As String
Dim cpf As String
BP = controlectform.nmbpbox.Value
cpf = controlectform.nmcpfbox.Value
If controlectform.optbp.Value = True Then
Call SelectNome("controle", "NOME", "BP")
Exit Sub
ElseIf controlectform.optcpf.Value = True Then
Call SelectNome("controle", "NOME", "cpf")
Exit Sub
Else
MsgBox "Selecione a opção de consulta!", vbCritical
End If
End Sub
The function takes the parameters and assembles the query query in the database through the sql variable. This is where Where is. But it does not work.
Function SelectNome(Tabela As String, Campo As String, Criterios As String) As Variant
Dim NOMEDB As Variant
Dim sql As String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="_
& enderecoDB & ";Jet OLEDB:Database"
cn.Open
Set rs = New ADODB.Recordset
sql = "SELECT " & Table & "." & Field & "From " & Table & "Where " & Criteria & ";"
rs.Open sql, cn
If Not rs.EOF Then
Do While Not rs.EOF
NOMEDB = rs(0)
rs.MoveNext
Loop
End If
cn.Close
controlectform.nomecolaboradorbox.Value = NOMEDB
End Function
I mean, I call Function:
Call Selectnome()
And I pass the arguments following the order, Table, Field, Criterion. In this case:
Call Selectname("control", "NAME", "BP")
My criteria is Where BP.
The way it’s written, man
While
will go through all the records of your query and only when you reach the end of the table (EOF
) theloop
will be terminated and then the value of NOMEDB copied to the control. The problem is in thewhere
that’s not working? That’s it?– Ismael
Exactly. The data is all formatted as text. But Where does not work at all.
– Humberto Faria
Then post your
where
to understand it.– Ismael