borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Next Topic (DataGrid Problem -- Urgent) New Topic New Poll Post Reply
AndreaVB Forum : Database : error in find record
Poster Message
noknok
Level: Big Cheese

Registered: 23-11-2007
Posts: 26

icon error in find record

This is the code in my query

'this i put in my module

'General Declaration

Public Const DBNAME = "medata.mdb"


Public Sub OpenDB()
Dim cn As New ADODB.Connection
Dim DBPATH As String
DBPATH = App.Path & "\" & DBNAME
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPATH

End Sub

'in my search form this are the code

Option Explicit
Dim rs As ADODB.Recordset
Dim cn As ADODB.Connection

Private Sub Form_Load()
Call OpenDB
End Sub

Private Sub fnd_Click()
Dim myname As String
myname = InputBox("Please Type The Name Here")
Set cn = New Connection
Set rs = New Recordset

rs.Open "Select * from biodata where lname like '" & myname & "%'"
searchlist.Clear
Do While Not rs.EOF
searchlist.AddItem (rs("lname") & "" & "," & rs("fname"))
rs.MoveNext
Loop
rs.Open
Set rs = Nothing
Set cn = Nothing
End Sub

and i got the following error

The connection cannot be used to perform this operation. It is either closed or Invalid in this  context

and when i click debug it goes to this line

rs.Open "Select * from biodata where lname like '" & myname & "%'"



[Edited by noknok on 22-01-2008 at 05:36 AM GMT]

22-01-2008 at 05:34 AM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 985
icon Re: error in find record

Fixes in BOLD


Public Const DBNAME = "medata.mdb"
public cn As New ADODB.Connection


Public Sub OpenDB()
Dim DBPATH As String
DBPATH = App.Path & "\" & DBNAME
set cn = new ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPATH

End Sub

'in my search form this are the code

Option Explicit
Dim rs As ADODB.Recordset
[b ' Dim cn As ADODB.Connection


private sub Form_Unload
set cn=nothing
end sub


Private Sub Form_Load()
'Call OpenDB NEVER USE CALL
OpenDB
End Sub

Private Sub fnd_Click()
Dim myname As String
myname = InputBox("Please Type The Name Here")
Set cn = New Connection
Set rs = New Recordset

rs.Open "Select * from biodata where lname like '" & myname & "%'", cn
searchlist.Clear
Do While Not rs.EOF
searchlist.AddItem (rs("lname") & "" & "," & rs("fname"))
rs.MoveNext
Loop
rs.Open
Set rs = Nothing
[b REM Set cn = Nothing
End Sub

and i got the following error

The connection cannot be used to perform this operation. It is either closed or Invalid in this  context

and when i click debug it goes to this line

rs.Open "Select * from biodata where lname like '" & myname & "%'"




Hope this helps,
Kieron


[Edited by admin on 24-01-2008 at 08:02 AM GMT]

____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

22-01-2008 at 07:30 AM
View Profile Send Email to User Show All Posts | Quote Reply
noknok
Level: Big Cheese

Registered: 23-11-2007
Posts: 26
icon Re: error in find record

still the same Problem, if im not going to put the code in module it work fine but when i code it in module i got that error and i forgot to say that i'm Using MDI and my search box is a child is that affect the code?

[Edited by noknok on 22-01-2008 at 08:29 AM GMT]

22-01-2008 at 08:17 AM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 985
icon Re: error in find record

Put a breakpointon the line in form_load "opendb", and make sure it has executed before you attempt to use it.


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

23-01-2008 at 07:40 AM
View Profile Send Email to User Show All Posts | Quote Reply
noknok
Level: Big Cheese

Registered: 23-11-2007
Posts: 26
icon Re: error in find record

I dont know what u mean a breakpoint?

and thats exactly one thing i like to ask how do i know that before i run the form the module will be load first then followed by OPenDB in my module

23-01-2008 at 07:59 AM
View Profile Send Email to User Show All Posts | Quote Reply
noknok
Level: Big Cheese

Registered: 23-11-2007
Posts: 26
icon Re: error in find record

I dont know what u mean a breakpoint?

and thats exactly one thing i like to ask how do i know that before i run the form the module will be load first then followed by OPenDB in my module


i dont realy understand the connection of my database is already open and connect base in the module so why its not running

where when why

23-01-2008 at 08:02 AM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 985
icon Re: error in find record

The form that has the code "opendb" in it needs to be visible before you attempt to use the connection.


Research the topics "debugging visual basic" to learn about breakpoints and debugging.

To add a breakpoint:
Go to the line of code in "public sub OpenDB", press F9. The line will turn RED.
Your application will now stop running when it gets to this line.
Run your app.
If this line has not been reached, you are not calling OpenDB.

Press F9 to turn this breakpoint off again.
Press F8 to step through each line of your code instead of running it
Press F5 to run your app.

I suggest you stop your application, then press F8 to see the lines of code executing one at a time (press F8 to move to next line of code).


____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

23-01-2008 at 01:05 PM
View Profile Send Email to User Show All Posts | Quote Reply
noknok
Level: Big Cheese

Registered: 23-11-2007
Posts: 26
icon Re: error in find record

I use MDI in my program,

now i have 3 form

main.frm  --- Parent or the main 1
biodata.frm -- child form
search.frm -- child form

in my main frm  i have a menu and my menu coposed of File and Find, when i click File and select open it will show my biodata.frm and if i click my Find the search.frm will appear

running the F8 to look where my code goes


when Search form Load declare Opendb it connects to my opendb in the module and it read it then when after reading it goes back to my main form then end the read

why?

P.S  its getting interesting and i learn more better than reading those guide ^^ thanks a lot kieron

[Edited by noknok on 24-01-2008 at 02:06 AM GMT]

24-01-2008 at 02:03 AM
View Profile Send Email to User Show All Posts | Quote Reply
JLRodgers
Level: Moderator

Registered: 04-04-2002
Posts: 1629
icon Re: error in find record

Code runs until the End Sub/function if that's what your wondering.

So...

if mdlMain opens MDIMain, that opens Form1 (upon MDIMain's load), that immediately opens form2, all of form 2's events will run, then the rest of the form1's load stuff, followed by all of MDIMain's load stuff, followed by the rest of the mdlMain's code (after the MDIMain's Show or Load)

basicall... if you have

a calling b calling c calling.......x calling y calling z, once z completes, it'll return to the procedure that called it and continue running code in that routine until there's no code left to run (i.e. it hit the original calling routine's "End Sub / End Function".

____________________________
Everywhere's Local (classifieds, job postings, & more for everycity in the world - user entered)

24-01-2008 at 02:42 PM
View Profile Send Email to User Show All Posts Visit Homepage | Quote Reply
noknok
Level: Big Cheese

Registered: 23-11-2007
Posts: 26
icon Re: error in find record

Thanks to Kieron that realy gives me a big big help and expecially with his simple code andyet its so effective,

and also to JLRodgers and yronium,

this problem is solve and ill look forwrd for some big database query like yronium post , after a long week studying at kieron code why i still got problem, accidentally trying and error and F8 and F9 what Kieron said i merely understand a bit but still continue studying to be a better 1

this is the code

' Code in Module
Public Const DBNAME = "medata.mdb"
Public cn As New ADODB.Connection


'Public byname As Boolean
'Public notbyname As Boolean
Public Const adOpenDynamic = 2       ' Open with full dynamic record set
Public Const adLockReadOnly = 1      ' Open database read only
Public Const adLockOptimistic = 3    ' lock the record when update
Public Const adLockPessimistic = 2   ' Lock the record as soon as start editing
Public Const adCmTable = 2

Public Sub OpenDB()
Dim DBPATH As String
DBPATH = App.Path & "\" & DBNAME
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPATH & ";"
end sub


' Code in my form

Private Sub cmdcancel_Click(Index As Integer)
Unload Me
End Sub

Private Sub cmdsaudi_Click(Index As Integer)
Dim rs As Recordset
    myname = InputBox("please type the start of the lastname you are looking for")
    myname = UCase(myname)
    Set rs = New Recordset
    rs.Open "select * from biodata where lname like '" & myname & "%'", cn
    List1.Clear
    Do While Not rs.EOF
    List1.AddItem (rs("lname") & "" & "," & rs("fname") & "")
    rs.MoveNext
    Loop
    rs.Close
    Set rs = Nothing

End Sub

Private Sub Form_Load()
OpenDB
End Sub


hope this code will help other Beginners like me

Credit thanks to Kieron

P.S. I have more question to go ^^

31-01-2008 at 05:50 AM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : Database : error in find record
Next Topic (DataGrid Problem -- Urgent) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder