473,473 Members | 4,189 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using For Loop through an access Database

25 New Member
Hello everyone

Am using a For Loop to access records from my database but the code opens instead of a specific record base on values passed by the user. These values a fields in the database which i want to make keys.

This a bit of the code
Vb 6 and Microsof Access

code Vb:

If rs.RecordCount = 0 Then Exit Sub
rs.MoveFirst
For i = 1 To rs.RecordCount ' Read all the records from database

cbosch = rs("CentNo")
Cand_No = rs("IndexNo")
Sex = rs("Sex")
DOB = rs("DOB")
Cnd_Name = rs("CandName")
Grd11 = rs("Grd1")
Subj2 = rs("Subj2")
Grd12 = rs("Grd2")
Subj3 = rs("Subj3")
Grd13 = rs("Grd3")
Subj4 = rs("Subj4")
Grd14 = rs("Grd4")
Subj5 = rs("Subj5")
Grd15 = rs("Grd5")
Subj6 = rs("Subj6")
Grd16 = rs("Grd6")
Subj7 = rs("Subj7")
Grd17 = rs("Grd7")
Subj8 = rs("Subj8")
Grd18 = rs("Grd8")



'validate the Key fields (Examseries, year, candNo and index no.)
Exam_Sr = rs("ExamSeries")
If Exam_Sr = "" Then
MsgBox ("Pls enter the correct Exam Serie"), vbOKOnly
ElseIf Exam_Sr = 1 Then
frmListing.Exam_Series.Caption = "May/June"
Else
Exam_Sr = 2
frmListing.Exam_Series.Caption = "Nov/Dec"

Me.Exam_Sr.SetFocus
Me.Refresh
End If

Exam_Yr = rs("ExamYear")
If Exam_Yr = "" Then
MsgBox ("Please Enter the exam year"), vbOKOnly

Me.Exam_Yr.SetFocus
End If
It prints the fields on to another form after getting them
'Show a modal form named frmListing.
frmListing.Show vbModal, Me

rs.MoveNext
Next i

code ends
Nov 20 '07 #1
14 9759
AHMEDYO
112 New Member
HI....

i understand your code and every thing is ok, but i didnt find your question, or i miss something???

Best Regards
Nov 20 '07 #2
gilsygirl
25 New Member
Thanks for the reply

The problem is this

the for loop am using counts all the records and the prints all of the records from the database.

What i can't do is to be able to choose record by record using the the Candidate No, Year of exam and centre No.

I just don't how to use these three values as a key to search for a record.

Thanks for your help.

Newbie
Nov 29 '07 #3
Killer42
8,435 Recognized Expert Expert
One possibility would be to check these fields inside the For loop, to decide whether to process the record.

Generally though, you would probably want to use a WHERE clause in your SQL to determine which records are placed in the recordset. Can you show us how rs was created?
Nov 29 '07 #4
gilsygirl
25 New Member
One possibility would be to check these fields inside the For loop, to decide whether to process the record.

Generally though, you would probably want to use a WHERE clause in your SQL to determine which records are placed in the recordset. Can you show us how rs was created?
Thanks for the reply. This is where i create the recordset.
Expand|Select|Wrap|Line Numbers
  1. Public Function initdb()
  2. On Error GoTo error
  3. Set ws = DBEngine.Workspaces(0)
  4. Set db = ws.OpenDatabase("c:\resultchecker.mdb")
  5. Set rs = db.OpenRecordset("candres", dbOpenTable)
  6. Exit Function
This is the for loop and the select statement where i check the records.

Expand|Select|Wrap|Line Numbers
  1. If rs.RecordCount = 0 Then Exit Sub
  2.      rs.MoveFirst
  3.    For i = 1 To rs.RecordCount ' Read all the records from database
  4.  
  5.    sqlStmt = "select * From candres where ExamSeries='" & Exam_Sr & "'and ExamYear='" & Exam_Yr & "'and CentNo='" & cbosch & "' and IndexNo = '" & Cand_No & "'"
  6.     rs.OpenRecordset sqlStmt, dbOpenTable this where i get the error
  7.    (Datatype conversion error)
  8.  
  9.        If Not (rs.EOF) Then
  10.           frmCandResult.Show vbModal, Me
  11.        Else
  12.           MsgBox "sorry not a perfect match"
  13.     End If
Thanks a lot for all the help i really want this work.
gilsygirl (Newbie)
Nov 30 '07 #5
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

Check this Modified code.
Remove the If Condition and the For Loop:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. sqlStmt = "select * From candres where ExamSeries='" & Exam_Sr & "'and ExamYear='" & Exam_Yr & "'and CentNo='" & cbosch & "' and IndexNo = '" & Cand_No & "'"
  4. Set rs = db.OpenRecordset(sqlStmt, dbOpenDynaset)
  5. If RS.RecordCount>0 Then
  6.      frmCandResult.Show vbModal, Me
  7. Else
  8.     MsgBox "sorry not a perfect match"
  9. End If
  10.  
Regards
Veena
Nov 30 '07 #6
gilsygirl
25 New Member
Hello QVeena72

Thanks for the code. I tried it but the problem is i have already set db in the General section. In this code we are trying to set it again.
Set rs = db.OpenRecordset(sqlStmt, dbOpenDynaset)
If RS.RecordCount>0 Then
frmCandResult.Show vbModal, Me


What do think i should do.

Thanks for ur help.
Dec 11 '07 #7
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

I guess you mean "RS" Object, you are opening in General Section.
Declare Another Recordset (say RSNew) and open RSNew, with the above statament..

Regards
Veena
Dec 11 '07 #8
Killer42
8,435 Recognized Expert Expert
If RS is already defined and you don't want to change it, probably your best bet is to create a temporary recordset as Veena suggested, which includes just the records you want to play with.

Alternatively, you could simply use an IF test within the loop. Something like...
Expand|Select|Wrap|Line Numbers
  1. Dim FoundAny As Boolean
  2. If rs.RecordCount > 0 Then
  3.   rs.MoveFirst
  4.   Do Until rs.EOF
  5.     If rs("ExamSeries") = Exam_Sr _
  6.     And rs("ExamYear") = Exam_Yr _
  7.     And rs("CentNo") = cbosch _
  8.     And rs("IndexNo") = Cand_No Then
  9.       frmCandResult.Show vbModal, Me
  10.       FoundAny = True
  11.     End If
  12.     rs.MoveNext
  13.   Loop
  14. End If
  15. If Not FoundAny Then
  16.   MsgBox "Not a erfect match"
  17. End If
You might also want to investigate the FindFirst and FindNext methods.

You really don't want to try and change the recordset while you're in the middle of processing it (as in the rs.OpenRecordset in post #5).

One other thing, which may have caused you some confusion. When a recordset is opened, you cannot trust the RecordCount property to tell you how many records there are - only whether there are any. It starts out as either 0 or 1, I believe. If you want the actual count of records, you need to do a MoveLast to force the DB engine to read them all.
Dec 12 '07 #9
gilsygirl
25 New Member
Can any body tell me what wrong with this code. Been trying for ages but it only displays the first record from th database.


Expand|Select|Wrap|Line Numbers
  1. If rs.RecordCount = 0 Then Exit Sub
  2. For i = 1 To rs.RecordCount ' Read all the records from database
  3.  
  4.   Do While rs.EOF
  5.     rs.MoveFirst
  6.     sqlStmt = "select * From candres where ExamSeries='" & Exam_Sr & "' and ExamYear='" & Exam_Yr & "' and CentNo='" & Cent_No & "' and IndexNo='" & Index_No & "'"
  7.  
  8.   Loop
  9.   If ((rs.ExamSeries = Exam_Sr) And (rs.ExamYear = Exam_Yr) And (rs.CentNo = Cent_No) And (rs.IndexNo = Index_No)) Then
  10.     Call dispres
  11.   End If
  12.  
  13.   If rs.EOF <> True Then
  14.     MsgBox "No records found"
  15.   End If
  16.  
  17.   frmListing.Show vbModal, Me
  18.  
  19. Next i
  20. rs.MoveNext
  21. End Sub
just don't know what is wrong with the code.
Mar 26 '08 #10
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

Why you are Moving First (4th line) ...?
Remove that part and Just before Loop, Write RST.MoveNext

Regards
Veena
Mar 26 '08 #11
Killer42
8,435 Recognized Expert Expert
I'm going to try rearranging the code so it makes a bit more sense. The looping seems all mixed up. (That Do While loop, especially, looks as though it's simply being used in place of an IF statement).

Note that I'm not entirely familiar with what you're trying to do, so don't take this as gospel...
Expand|Select|Wrap|Line Numbers
  1. If rs.RecordCount = 0 Then Exit Sub
  2.  
  3. AnyFound = False ' (AnyFound would be declared as Boolean)
  4. rs.MoveFirst
  5. Do Until rs.EOF
  6.   If (rs.ExamSeries = Exam_Sr) And (rs.ExamYear = Exam_Yr) And (rs.CentNo = Cent_No) And (rs.IndexNo = Index_No) Then
  7.     AnyFound = True
  8.     Call dispres
  9.   End If
  10.   rs.MoveNext
  11. Loop
  12.  
  13. If Not AnyFound Then
  14.   MsgBox "No records found"
  15. End If
  16.  
  17. frmListing.Show vbModal, Me
  18.  
Mar 26 '08 #12
gilsygirl
25 New Member
Hi,

Why you are Moving First (4th line) ...?
Remove that part and Just before Loop, Write RST.MoveNext

Regards
Veena
Hi Qveen72
I have tried putting moving that line but it is still not working.
the Cent_No text box keeps changing when the mouse is moved but dont not call the disprec fuction.
Just can figure the error.
Mar 27 '08 #13
QVeen72
1,445 Recognized Expert Top Contributor
Hi,

Probably the IF condition is not being satisfied...

Expand|Select|Wrap|Line Numbers
  1. If (rs.ExamSeries = Exam_Sr) And (rs.ExamYear = Exam_Yr) And (rs.CentNo = Cent_No) And (rs.IndexNo = Index_No) Then
  2.  
Regards
Veena
Apr 2 '08 #14
Killer42
8,435 Recognized Expert Expert
gilsygirl, you need to get familiar with the debugging tools built into VB. You can track exactly what the code is doing, statement by statement, and examine/modify variables on the fly, and so on. This will be your best weapon in attacking this bug.
Apr 3 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: BCS | last post by:
I have a web site in which the site administrator can input information to a database through a web form. The information then gets displayed on ASP pages. One field is a large text field. Of...
6
by: ALthePal | last post by:
Hi, I'm not sure if we are able to or even how to loop through the web forms in a VB.NET project during design time. In MSAccess we are able to go through the database -> forms collection and...
3
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
17
by: Danny J. Lesandrini | last post by:
The following code works with a standard MDB to navigate to a particluar record (with a DAO recordset, of course) but it's giving me problems in an ADP I'm working on. Dim rs As ADODB.Recordset...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
8
by: doomx | last post by:
I'm using SQL scripts to create and alter tables in my DB I want to know if it's possible to fill the description(like in the Create table UI) using these scripts. EX: CREATE TABLE(...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
4
by: James | last post by:
I have a VB windows forms application that accesses a Microsoft Access database that has been secured using user-level security. The application is being deployed using No-Touch deployment. The...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.