hello,i have problem with running really slow when it is retreaving records from table to display only the records I need. I am using access (.mdb) for my database and I use ADO to connect to the database on visual basic .most of the time my computer will hang when i retreaving data and my computer will become extremely slow until i need to restart computer.Does anyone know how to go about making data retreaval more efficient or faster, either through tweaking my code or tweaking the db itself using MS Access ?please help Thanks!
VB Code:
Private m_rs As ADODB.Recordset ' Recordset tool
Private Sub CmdAdd_Click(Index As Integer)
On Error Resume Next
With m_rs
.Fields("Location") = ComboLocation.Text
.Fields("DatePurchase") = DTPpurchase
.Fields("Item") = ComboItem.Text
.Fields("Detail") = Txtitem2.Text
.Fields("IDNumber") = Txtidnumber.Text
.Fields("Price") = Txtprice.Text
.Fields("Status") = Combostatus.Text
.Fields("DateScrap") = DTPScrap
.Update
Call clear
MsgBox "Record Saved", vbInformation, "Record is Saved"
'End If
End With
End Sub
'Delete Record
Private Sub cmddelete_Click(Index As Integer)
On Error Resume Next
With m_rs
.Fields("Location") = ComboLocation.Text
.Fields("DatePurchase") = DTPpurchase
.Fields("Item") = ComboItem.Text
.Fields("Detail") = Txtitem2.Text
.Fields("IDNumber") = Txtidnumber.Text
.Fields("Price") = Txtprice.Text
.Fields("Status") = Combostatus.Text
.Fields("DateScrap") = DTPScrap
.Fields("Deletedate") = Txtdeldate.Text
Call clear
If MsgBox("Are you sure you wan't to delete record", vbOKCancel + vbExclamation, "Deleting Record") = vbOK Then
When doing a Select query, specify the field name not *. Well, in some case you need to and try to avoid using On Error Resume Next if necessary. It will be easier to debug.
As a side note, I'd ditch those 'On Error Resume Next' statements in the Add and Delete Subs. You aren't doing anything that should cause you to have to Resume next per se (use On Error Goto xxxx).
bruce fox:
i has try your method to use 'On Error Resume Next' statements in the Add and Delete Subs.but the problem still remind the same...extremely slow until computer and need to reboot agian.
Ok here's what you need to do. Remove all On Error Resume Next and see if there's an error.
If there is, then post the offending line of code. In my opinion, there must be an error on your coding but since you have that Resume Next, chances are you won't find the error and be impossible to debugged.
If they're properly coded, even if you load more than 6,000 records say 30k plus, it'll not get to the point of system freeze. There must be something in there that you can't find.
if you run your program continuously does it just get slower? or is it if you keep restarting it?
do you have code in your form unload or form query unload events?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
westconn1:
once i run project to retrieve record my computer will get extremely slow until hang for few sec and i need to reboot agian to gain back to normal speed.ya ,from database have database query events.
As already mentioned above, specify your fields (instead of "Select *"), do not use "On Error Resume Next" (unless you have a good reason - which you certainly dont), and specify properties when working with objects (such as .Value for fields, and possibly for your date pickers too).
There are further improvements that can be made to the speed of your SQL statement, but we need to clean up your coding issues a bit first.
What is OpenRecordset? Presumably this is a function which you have written, and not shown.
What is gUserId? Presumably a global variable, but has it been set appropriately before the form loads? (note that in your main SQL statement, you dont have ' around it for the UserRoles part)
si_the_geek:
you are right the OpenRecordset written are not shown. gUserId is Id from database users table Id.At module1 you can see all the variable there.please guide i'm new in VB.Thanks!
I think they both OpenRecordset's are the same one, just posted twice.
The setting of rs is done, but it is very dubious - it is done by "Dim As New"
Instead it should be done with a Set, and you should clear it when possible (to avoid memory issues etc), eg:
VB Code:
..
[U]Dim rs As Recordset
Set rs = New Recordset[/U]
If OpenConn Then 'aviod time ping out
On Error GoTo TrackErr
rs.Open strsql, conn, CursorType, LockType
End If
Set OpenRecordset = rs
[U]Set rs = Nothing[/U]
..
In Sub Main you have a very bad piece of code - a single word that should always be avoided, which is End. In this case you should replace it with Exit Sub (as the program ends safely when there is no more code to run, and no forms open), eg:
VB Code:
If Not gLogined Then Exit Sub
I assume frmLogin basically (after user input) just calls DoLogin, correct?
In several of your SQL statements you have used "like" instead of "=", is there a reason for that?
si_the_geek:
i has been try the method you mention above,but the problem still remain the same,records retreive extremely slow until computer hang need to reboot . ya, frmlogin is affert user input click Ok to login will calls Dologin and SQL statements used "like" instead of "=",am i used the wrong method?
below is frmlogin code:
VB Code:
Private Sub cmdOK_Click()
'check corerct password to login
If Not DoLogin(txtUserName.Text, txtPassword.Text) Then
MsgBox "Please Enter Correct Password£¡" & gErrDescription, vbCritical, "Warning"
Exit Sub
End If
Unload Me
End Sub
Thanks!
Last edited by gracehskuo; Jan 12th, 2007 at 07:41 PM.
Try putting breakpoints in your code at the start of each module. See which ones you hit quickly. Then, when you don't hit the next one, you'll know the previous one is where your problem is.
Then step through the code in that module to see which line causes the delay.
This is kind of a brute force method to find the problem, but should work to identify it.
Ok, then when you get to that module, step through that code to see which line causes the problem. You can do that when you hit the breakpoint in that module by using the debug menu, or, I think, hitting F8. That will walk you through the code line by line.
The problem could be in OpenConn, since you call that. Stepping through will help with that.
Per Si above, also change the way you declare rs.
salvelinus:
i have try the F8 method go through the code line by line.i can't found any problem at this moment.now is run the first and second time retreive record will get faster,but third time login to retreive record it will back very slow.
help please!
Thanks!
Last edited by gracehskuo; Jan 13th, 2007 at 01:29 AM.
Well, I'm going on vacation to Barbados, so probably can't work through this with you. But if it works ok first time, slows down later, you might want to look at memory resources.
First, obviously, is do you have enough memory on your machine for what you want to do? This isn't as big a problem as it used to be, but I still see machines with 32 mb memory trying to run more modern apps.
Second, do you close objects and Set them to Nothing - rs, etc - when you're done with them? If performance gets worse with each run, this would be a good place to look.
If it's not too large, and you're allowed to do it, you might want to zip up your db and post it here, so people can check it out.
salvelinus:
Gratitude your suggestion.my laptop memory is 1gb,so i think may not a problem to run the project.i will check agian the close object and Set them to Nothing.
by the way how to post the zip file here?
Yes, 1 gb of memory should work
To post an attachment, first zip your file using Winzip or whatever program you may have to zip files. Then when you click "Reply", towards the bottom of the screen (scroll if necessary) you'll see a section to "Attach Files" with a button "Manage Attachments"
Okay so I've tried your project and see a lot of problems.
Memory Issues ( took more than 1G of memory )
You have to fix your code and be sure to Set your objects or recordset to Nothing. Binding a database is not of my choice.
VB Code:
Set m_rs = OpenRecordset("select * from [Inventory]" & vbCrLf & _
"where ([Location] in (" & vbCrLf & _
" select [DepartmentName] from [Department]" & vbCrLf & _
" where [Id] in (" & vbCrLf & _
" select [DepartmentID] from [UserDepartment]" & vbCrLf & _
I can't even log onto your system without system errors (Selected collating sequence not supported by the operating system).
BTW: Your database is designed all wrong. You use text fields for everything except date/time fields. Why do you use text fields for numbers? Indexing and searching will be horribly slow.
Last edited by randem; Jan 15th, 2007 at 01:56 AM.
randem:
can't logon to the system?mean you can't even open the project file?
you mean using text fields for numbers will cause the system run very slow?so that the system keep hang? i has change the number to number field,but it seem not much improve.
Thanks!
Last edited by gracehskuo; Jan 15th, 2007 at 03:13 AM.