A
Aya
Hi. I'm using an VBA code I've found on internet about looking for a
recordset. I decided to add it to the Search form I've created. The code I've
found it in this link:
http://www.functionx.com/vbaccess/howtoado/locaterecord.htm
The function of this code was when the user enters the ID in the textbox,
then when he/she clicks the Enter or Tab button, the information appears like
title, department, supervisor, etc.
When I run the form in VIEW, I wrote the ID correctly like it was in the
Search table, but it doesn't recognize the it. It always shows the message
box, instead of finding it and show it in the form.
I wrote the code exactly like the example, but with the name of the fields of
my table. Here's my code I've entered in the ID textbox of my Search form:
Private Sub ID_LostFocus()
Dim rstImpact_Safety_Analysis As ADODB.Recordset
Dim blnFound As Boolean
' This flag will allow us to know whether the item number was found
Dim fldItem As ADODB.Field
' Since we are only starting, we assume that no item number has been
found
blnFound = False
' If there is no value in the ID textbox, don't do nothing
If Me.ID = " " Then Exit Sub
Set rstImpact_Safety_Analysis = New ADODB.Recordset
rstImpact_Safety_Analysis.Open "SELECT * FROM Impact_Safety_Analysis
WHERE ID = ' " & _
ID & " ' ", _
CurrentProject.Connection, _
adOpenStatic, adLockReadOnly, adCmdText
With rstImpact_Safety_Analysis
' Check each record
While Not .EOF
' Check the name of the column
For Each fldItem In .Fields
' If the current column is ID
If fldItem.Name = "ID" Then
' Check its value
' If the current column holds the ID that the user
entered
If fldItem.Value = TÃtulo Then
' ... then get the record and display its values in
the controls
Me.ID = .Fields("ID")
Me.TÃtulo = .Fields("TÃtulo")
Me.Descripción = .Fields("Descripción")
Me.Fecha = .Fields("Fecha")
Me.Supervisor = .Fields("Supervisor")
Me.Departamento = .Fields("Departamento")
Me.Análisis_Por = .Fields("Análisis_Por")
Me.Adiestramiento = .Fields("Adiestramiento")
Me.Procedimientos = .Fields("Procedimientos")
Me.EPP_Recomendado = .Fields("EPP_Recomendado")
Me.Secuencia_de_Pasos = .Fields("Secuencia_de_Pasos")
Me.Riesgos = .Fields("Riesgos")
Me.Conducta_Segura = .Fields("Conducta_Segura")
Me.Secuencia_1 = .Fields("Secuencia_1")
Me.Riesgos_1 = .Fields("Riesgos_1")
Me.Conducta_Segura_1 = .Fields("Conducta_Segura_1")
Me.Secuencia_2 = .Fields("Secuencia_2")
Me.Riesgos_2 = .Fields("Riesgos_2")
Me.Conducta_Segura_2 = .Fields("Conducta_Segura_2")
Me.Secuencia_3 = .Fields("Secuencia_3")
Me.Riesgos_3 = .Fields("Riesgos_3")
Me.Conducta_Segura_3 = .Fields("Conducta_Segura_3")
Me.Secuencia_4 = .Fields("Secuencia_4")
Me.Riesgos_4 = .Fields("Riesgos_4")
Me.Conducta_Segura_4 = .Fields("Conducta_Segura_4")
Me.Secuencia_5 = .Fields("Secuencia_5")
Me.Riesgos_5 = .Fields("Riesgos_5")
Me.Conducta_Segura_5 = .Fields("Conducta_Segura_5")
Me.Secuencia_6 = .Fields("Secuencia_6")
Me.Riesgos_6 = .Fields("Riesgos_6")
Me.Conducta_Segura_6 = .Fields("Conducta_Segura_6")
Me.Secuencia_7 = .Fields("Secuencia_7")
Me.Riesgos_7 = .Fields("Riesgos_7")
Me.Conducta_Segura_7 = .Fields("Conducta_Segura_7")
Me.Secuencia_8 = .Fields("Secuencia_8")
Me.Riesgos_8 = .Fields("Riesgos_8")
Me.Conducta_Segura_8 = .Fields("Conducta_Segura_8")
' Set the found flag to true (we will use it later)
blnFound = True
End If
End If
Next
' In case you didn't find it, move to the next record
.MoveNext
Wend
End With
' If the ID was not found, ...
If blnFound = False Then
' ... let the user know, ...
MsgBox "The task you are looking for is not in our system."
' ... and reset the form
cmdReset_Click
End If
rstImpact_Safety_Analysis.Close
Set rstImpact_Safety_Analysis = Nothing
End Sub
Can anyone help me with this code??? I'm hoping to finish it this week before
I left for next week. If anyone have any other Search codes I can use, you're
always welcome to reply me back and I'll give it a try.
recordset. I decided to add it to the Search form I've created. The code I've
found it in this link:
http://www.functionx.com/vbaccess/howtoado/locaterecord.htm
The function of this code was when the user enters the ID in the textbox,
then when he/she clicks the Enter or Tab button, the information appears like
title, department, supervisor, etc.
When I run the form in VIEW, I wrote the ID correctly like it was in the
Search table, but it doesn't recognize the it. It always shows the message
box, instead of finding it and show it in the form.
I wrote the code exactly like the example, but with the name of the fields of
my table. Here's my code I've entered in the ID textbox of my Search form:
Private Sub ID_LostFocus()
Dim rstImpact_Safety_Analysis As ADODB.Recordset
Dim blnFound As Boolean
' This flag will allow us to know whether the item number was found
Dim fldItem As ADODB.Field
' Since we are only starting, we assume that no item number has been
found
blnFound = False
' If there is no value in the ID textbox, don't do nothing
If Me.ID = " " Then Exit Sub
Set rstImpact_Safety_Analysis = New ADODB.Recordset
rstImpact_Safety_Analysis.Open "SELECT * FROM Impact_Safety_Analysis
WHERE ID = ' " & _
ID & " ' ", _
CurrentProject.Connection, _
adOpenStatic, adLockReadOnly, adCmdText
With rstImpact_Safety_Analysis
' Check each record
While Not .EOF
' Check the name of the column
For Each fldItem In .Fields
' If the current column is ID
If fldItem.Name = "ID" Then
' Check its value
' If the current column holds the ID that the user
entered
If fldItem.Value = TÃtulo Then
' ... then get the record and display its values in
the controls
Me.ID = .Fields("ID")
Me.TÃtulo = .Fields("TÃtulo")
Me.Descripción = .Fields("Descripción")
Me.Fecha = .Fields("Fecha")
Me.Supervisor = .Fields("Supervisor")
Me.Departamento = .Fields("Departamento")
Me.Análisis_Por = .Fields("Análisis_Por")
Me.Adiestramiento = .Fields("Adiestramiento")
Me.Procedimientos = .Fields("Procedimientos")
Me.EPP_Recomendado = .Fields("EPP_Recomendado")
Me.Secuencia_de_Pasos = .Fields("Secuencia_de_Pasos")
Me.Riesgos = .Fields("Riesgos")
Me.Conducta_Segura = .Fields("Conducta_Segura")
Me.Secuencia_1 = .Fields("Secuencia_1")
Me.Riesgos_1 = .Fields("Riesgos_1")
Me.Conducta_Segura_1 = .Fields("Conducta_Segura_1")
Me.Secuencia_2 = .Fields("Secuencia_2")
Me.Riesgos_2 = .Fields("Riesgos_2")
Me.Conducta_Segura_2 = .Fields("Conducta_Segura_2")
Me.Secuencia_3 = .Fields("Secuencia_3")
Me.Riesgos_3 = .Fields("Riesgos_3")
Me.Conducta_Segura_3 = .Fields("Conducta_Segura_3")
Me.Secuencia_4 = .Fields("Secuencia_4")
Me.Riesgos_4 = .Fields("Riesgos_4")
Me.Conducta_Segura_4 = .Fields("Conducta_Segura_4")
Me.Secuencia_5 = .Fields("Secuencia_5")
Me.Riesgos_5 = .Fields("Riesgos_5")
Me.Conducta_Segura_5 = .Fields("Conducta_Segura_5")
Me.Secuencia_6 = .Fields("Secuencia_6")
Me.Riesgos_6 = .Fields("Riesgos_6")
Me.Conducta_Segura_6 = .Fields("Conducta_Segura_6")
Me.Secuencia_7 = .Fields("Secuencia_7")
Me.Riesgos_7 = .Fields("Riesgos_7")
Me.Conducta_Segura_7 = .Fields("Conducta_Segura_7")
Me.Secuencia_8 = .Fields("Secuencia_8")
Me.Riesgos_8 = .Fields("Riesgos_8")
Me.Conducta_Segura_8 = .Fields("Conducta_Segura_8")
' Set the found flag to true (we will use it later)
blnFound = True
End If
End If
Next
' In case you didn't find it, move to the next record
.MoveNext
Wend
End With
' If the ID was not found, ...
If blnFound = False Then
' ... let the user know, ...
MsgBox "The task you are looking for is not in our system."
' ... and reset the form
cmdReset_Click
End If
rstImpact_Safety_Analysis.Close
Set rstImpact_Safety_Analysis = Nothing
End Sub
Can anyone help me with this code??? I'm hoping to finish it this week before
I left for next week. If anyone have any other Search codes I can use, you're
always welcome to reply me back and I'll give it a try.