 |
|
 |
noknok Level: Big Cheese
 Registered: 23-11-2007 Posts: 26
|
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 |
|
|
noknok Level: Big Cheese
 Registered: 23-11-2007 Posts: 26
|
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 |
|
|
noknok Level: Big Cheese
 Registered: 23-11-2007 Posts: 26
|
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 |
|
|
noknok Level: Big Cheese
 Registered: 23-11-2007 Posts: 26
|
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 |
|
|
noknok Level: Big Cheese
 Registered: 23-11-2007 Posts: 26
|
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 |
|
|
JLRodgers Level: Moderator
 Registered: 04-04-2002 Posts: 1629
|
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 |
|
|
noknok Level: Big Cheese
 Registered: 23-11-2007 Posts: 26
|
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 |
|
|
|
|
 |
 |