Hello there, is been a while since I last posted here, mostly because I didn't have the time I needed to program. But now, I reentered the exciting world in Visual Basic, 'cause I have a lot of time now and my mom wants me to make her a database :lol:
Anyway, I'm using VB 6.0 and I thought it was going to be easier than it's becomed. It all started when I tried to add a Search command button to the database the Data Form Object Wizard built. I searched through the internet and the forums and found out about SQL and ADO and read some tutorials on them, even the one that is posted at the beginning of the VB forum and decided to make one from scratch. But now, although my code seems right, it won't return me any records. I want the records to be input into Text Boxes. Here's the starting code I'm using in the Form_Load:

Private Sub Form_Load()

Dim Pacdb As New ADODB.Connection
Dim Pacrs As New ADODB.Recordset
Dim Paccmd As New ADODB.Command

Pacdb.CursorLocation = adUseClient
Pacdb.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=C:\Documents and Settings\Owner\My Documents\PruebaPropac\HistoriasClínicas.mdb;Mode=Read|Write"
Pacdb.Open

With Paccmd
  .ActiveConnection = Pacdb
  .CommandText = "SELECT * FROM Pacientes ORDER BY Nombre;"
  .CommandType = adCmdText
End With

With Pacrs
  .CursorType = adOpenStatic
  .CursorLocation = adUseClient
  .LockType = adLockOptimistic
  .Open Paccmd
End With

Please help me, tell me what code I'm missing for the database to return each value in a predetermined text box.

Dani AI

Generated

Brief summary and practical fixes (adds to and ): the original Form_Load approach is fine, but the runtime error when clicking the Next button usually comes from scope/lifetime problems or from trying to reopen a recordset/connection that no longer exists. Keep a single Connection and a single Recordset instance that live for the form lifetime, populate your controls from that live recordset, and move the cursor instead of re-opening the set on every button click.

How to structure it (high level)

  • Declare the ADO objects at form/module level so every button can use them (for example, Private cn As ADODB.Connection and Private rs As ADODB.Recordset).

  • Open the connection and open the recordset once in Form_Load. After that, navigation buttons should call rs.MoveNext, rs.MovePrevious, etc., then update the textboxes from rs.Fields("FieldName").Value.

  • When assigning to textboxes always handle Nulls to avoid type errors:

    If IsNull(rs.Fields("FirstName").Value) Then txtFirstName.Text = "" Else txtFirstName.Text = rs.Fields("FirstName").Value

Search and filters

  • For a Search button you can either build a WHERE clause and re-open the recordset, or use rs.Find / rs.Filter against the existing set. If you build SQL, escape quotes in the user input (Replace(txtSearch.Text, "'", "''")) to avoid syntax errors.

Debugging and good practices

  • Use Debug.Print rs.State, Debug.Print rs.RecordCount, and check rs.BOF/rs.EOF when stepping through.
  • Add a reference to the Microsoft ActiveX Data Objects library appropriate for your environment.
  • Close and set objects to Nothing in Form_Unload.
  • Use a client cursor if you need accurate RecordCount and smooth client-side navigation.

These changes explain why the button error appeared and will make navigation and search reliable without repeatedly re-opening the database objects.

Recommended Answers

All 6 Replies

I forgot to ask if there was a way of doing this easier with the Data Form Wizard, and if so, how to add the search command.
Thx again XD

Hello,
I am also a new user , but i know the connectivity,
please try the code below ??

manojsah


Private Sub Form_Load()


Dim Pacdb As New ADODB.Connection
Dim Pacrs As New ADODB.Recordset
'Dim Paccmd As New ADODB.Command
Dim str As String


Pacdb.Open = "PROVIDER=Microsoft.Jet.OLEDB.3.51;Data Source=C:\Documents and Settings\Owner\My Documents\PruebaPropac\HistoriasClínicas.mdb;Mode=Read|Write"
str = "SELECT * FROM Pacientes ORDER BY Nombre;"
Pacrs.Open str, Pacdb, adOpenDynamic, adLockOptimistic
Pacrs.MoveFirst
text1.Text = (Pacrs!Attribute_name)

End Sub


Bye
manojsah

Hey man, thank you very much it really worked, except that you don't need the "=" after Pacdb.Open or it won't work, but everything else was a lot of help. Anyway, be aware of this thread because I will post more here if I have more problems with my database. Thanks a lot. :mrgreen:

Man, I can get it. What am I doing wrong? Now I want to see the next records using a button, just to see if they're there, but it returns me an error of:

"Arguments are of the wrong type, are out of acceptable range or are in conflict with one another"

When I press the button. Then, if I press the Debug option, it highlights the following part of the buttons code:

Private Sub Command1_Click()
Dim str As String
Dim Pacrs As New ADODB.Recordset

str = "SELECT * FROM Pacientes ORDER BY Nombre;"

'This is what the debug option highlights;
[U]Pacrs.Open str, Pacdb, adOpenDynamic, adLockOptimistic[/U] 

If Not Pacrs.EOF Then
  Pacrs.MoveNext
End If

Pacrs.Close

End Sub

The rest of the code is already been post by manojsah. Can anyone tell me what did I do wrong?
Thanks in advance for the help you can provide.

Dear Friend,
Assume rs as your recordset now change the below code with your recordset variable
i.e pacrs
make one Command Button and make the changes in below code.

here (rs!agency_no)
agency_no is your attribute.........
Ok bye
Enjoy Programming
take care
Manoj


Private Sub Command1_Click()

rs.MoveNext
If rs.EOF Then
rs.MoveLast
MsgBox ("On Last record")
Else
rs.MoveNext
MsgBox(rs!agency_no)

End If


End Sub

Thx again man, I can say now that this thread is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.