Getting data from database [need some help]
im a beginner programmer using Visual Basic 6
i have this program
For i = 1 To X
Load txtsubj(i)
txtsubj(i).Visible = True
txtsubj(i).Text = rssubCode!subject_code
rssubCode.MoveNext
txtsubj(i).Top = 1800 + t
txtsubj(i).Left = 1080
txtsubj(i).Width = 1500
txtsubj(i).Height = 255
t = t + 500
Next i
For i = (X + 1) To Y
Load txtGrade(i)
txtGrade(i).Visible = True
txtGrade(i).Text = rssubCode!grade
rssubCode.MoveNext
txtGrade(i).Top = 1800 + u
txtGrade(i).Left = 3080
txtGrade(i).Width = 1500
txtGrade(i).Height = 255
u = u + 500
Next i
my program works but the value of my txtsubj is not appropriate to the corresponding value of my txtGrade...
my problem is how can i make the value of my txtsubj (CHBE 113) and the value of my txtGrade (88.87) is appropriate to my database?
[975 byte] By [
disinwebe] at [2007-11-19 6:41:13]

# 1 Re: Getting data from database [need some help]
this is my full code
Dim rsave As New ADODB.Recordset, rssubCode As New ADODB.Recordset
Dim rsgrade As New ADODB.Recordset
Dim strave As String, t As Integer
Dim strsubCOde As String, strgrade As String
Dim i As Integer, X As String, Y As String
Dim reccount As Long
If rsave.State = -1 Then
rsave.Close
End If
strave = "SELECT sy, course, COUNT(subject_code) AS subject_count From average_table WHERE (studentno = 'WL3-0755') AND (sy='1st Semester S.Y. 2004-2005') GROUP BY sy, course"
rsave.ActiveConnection = dbstudentcnn
rsave.Open strave
strsubCOde = "SELECT subject_code, grade FROM average_ WHERE studentno='" & txtstudno(0).Text & "' AND sy='" & cmbSY.Text & "'"
rssubCode.ActiveConnection = dbstudentcnn
rssubCode.Open strsubCOde
X = rsave!subject_count
Y = rsave!subject_count + X
For i = 1 To X
Load txtsubj(i)
txtsubj(i).Visible = True
txtsubj(i).Text = rssubCode!subject_code
rssubCode.MoveNext
txtsubj(i).Top = 1800 + t
txtsubj(i).Left = 1080
txtsubj(i).Width = 1500
txtsubj(i).Height = 255
t = t + 500
Next i
For i = (X + 1) To Y
Load txtGrade(i)
txtGrade(i).Visible = True
txtGrade(i).Text = rssubCode!grade
rssubCode.MoveNext
txtGrade(i).Top = 1800 + t
txtGrade(i).Left = 3080
txtGrade(i).Width = 1500
txtGrade(i).Height = 255
t = t + 500
Next i
# 3 Re: Getting data from database [need some help]
If your Select statement is returning more than 1 record, then you would need to loop through the recordset if you wanted each to be populated correctly.
Try something like this. You may need to adjust the indexes (i) a little bit. (NOTE: You do not need the RecordCount from rsave for this to work, that's why I deleted it.)
Dim rssubCode As New ADODB.Recordset
Dim rsgrade As New ADODB.Recordset
Dim strave As String, t As Integer
Dim strsubCOde As String, strgrade As String
Dim i As Integer, X As String, Y As String
Dim reccount As Long
strsubCOde = "SELECT subject_code, grade FROM average_ WHERE studentno='" & txtstudno(0).Text & "' AND sy='" & cmbSY.Text & "'"
rssubCode.ActiveConnection = dbstudentcnn
rssubCode.Open strsubCOde
i = 0
Do Until rssubCode.EOF
i = i + 1
Load txtsubj(i)
txtsubj(i).Visible = True
txtsubj(i).Text = rssubCode!subject_code
'rssubCode.MoveNext 'This should have been deleted, thanks JonnyPoet
txtsubj(i).Top = 1800 + t
txtsubj(i).Left = 1080
txtsubj(i).Width = 1500
txtsubj(i).Height = 255
Load txtGrade(i)
txtGrade(i).Visible = True
txtGrade(i).Text = rssubCode!grade
'rssubCode.MoveNext 'This should have been deleted, thanks JonnyPoet
txtGrade(i).Top = 1800 + t
txtGrade(i).Left = 3080
txtGrade(i).Width = 1500
txtGrade(i).Height = 255
t = t + 500
rssubCode.MoveNext
Loop
You can also use rssubcode.RecordCount to get the number of records returned, but you need to include the following code (right before rssubcode.Open) in order for it to return an accurate RecordCount.
Set rssubcode= New ADODB.Recordset
rssubcode.CursorType = adOpenDynamic
rssubcode.CursorLocation = adUseClient
# 4 Re: Getting data from database [need some help]
Hi Guys
I think malleyo's code will help you a lot to solve your problem, but what I see are three times where he calls rssubCode.MoveNext and I'm frightended this is a type error from changing your basic code.
I think it will work if you delete them and use only the last one. Please check if I'm right or if I 'm wrong.
Jonny Poet :wave:
(I put the lines I mean they should be deleted to green)
Dim rssubCode As New ADODB.Recordset
Dim rsgrade As New ADODB.Recordset
Dim strave As String, t As Integer
Dim strsubCOde As String, strgrade As String
Dim i As Integer, X As String, Y As String
Dim reccount As Long
strsubCOde = "SELECT subject_code, grade FROM average_ WHERE studentno='" & txtstudno(0).Text & "' AND sy='" & cmbSY.Text & "'"
rssubCode.ActiveConnection = dbstudentcnn
rssubCode.Open strsubCOde
i = 0
Do Until rssubCode.EOF
i = i + 1
Load txtsubj(i)
txtsubj(i).Visible = True
txtsubj(i).Text = rssubCode!subject_code
'rssubCode.MoveNext
txtsubj(i).Top = 1800 + t
txtsubj(i).Left = 1080
txtsubj(i).Width = 1500
txtsubj(i).Height = 255
Load txtGrade(i)
txtGrade(i).Visible = True
txtGrade(i).Text = rssubCode!grade
'rssubCode.MoveNext
txtGrade(i).Top = 1800 + t
txtGrade(i).Left = 3080
txtGrade(i).Width = 1500
txtGrade(i).Height = 255
t = t + 500
rssubCode.MoveNext
Loop
(In between corrected by malleyo himself so lets go to another problem)
:wave: Thx to malleyo for Acknowledgement. It was obviously, that this was only a copyerror.
I have seen you worked in your code with x and y
Also when I looked to your design I see more then one students ( William ...)
So if you want to do this with a column for each student you have to modify the code. lets look what we can do.
Dim rssubCode As New ADODB.Recordset
Dim rsgrade As New ADODB.Recordset
Dim strave As String, t As Integer
Dim strsubCOde As String, strgrade As String
Dim i As Integer, X As String, Y As String
Dim reccount As Long
Dim iStudentsCount as Integer
Dim iStudentNr as Integer
strsubCOde = "SELECT subject_code, grade FROM average_ WHERE studentno='" & txtstudno(0).Text & "' AND sy='" & cmbSY.Text & "'"
rssubCode.ActiveConnection = dbstudentcnn
rssubCode.Open strsubCOde
i = 0
t = 0
' First we show all the txtsubj's
Do Until rssubCode.EOF
i = i + 1
Load txtsubj(i)
txtsubj(i).Visible = True
txtsubj(i).Text = rssubCode!subject_code
'rssubCode.MoveNext
txtsubj(i).Top = 1800 + t
txtsubj(i).Left = 1080
txtsubj(i).Width = 1500
txtsubj(i).Height = 255
' and his Grades just as before
Load txtGrade(i)
txtGrade(i).Visible = True
txtGrade(i).Text = rssubCode!grade
txtGrade(i).Top = 1800 + t
txtGrade(i).Left = 3080
txtGrade(i).Width = 1500
txtGrade(i).Height = 255
t = t + 500
rssubCode.MoveNext
Loop
rssubCode.Close strsubCOde
' now we go back again and take the next student
'here you will have to do a SQL to find out how much students you have.
'I dont have your database so you will have to do that yourself put that
' data in iStudentsCount. For this Example I set iStudentsCount manually to 3.
'Edit that for your needs.
iStudentsCount = 3
t = 0
iStudentNr = 0
iLeft = 3080
Do
iLeft =iLeft + 2000
iStudentNr = iStudentNr +1
'finished after last Student
If iStudentNr >= iStudentsCount then exit do
strsubCOde = "SELECT subject_code, grade FROM average_ WHERE studentno='" & Cstr(iStudentNr) & "' AND sy='" & cmbSY.Text & "'"
rssubCode.ActiveConnection = dbstudentcnn
rssubCode.Open strsubCOde
rssubCode.MoveFirst
Do Until rssubCode.EOF
i = i+1
Load txtGrade(i)
txtGrade(i).Visible = True
txtGrade(i).Text = rssubCode!grade
txtGrade(i).Top = 1800 + t
txtGrade(i).Left = iLeft
txtGrade(i).Width = 1500
txtGrade(i).Height = 255
t = t + 500
rssubCode.MoveNext
Loop
Loop
I think in that form it should basically work for more then one student. I'm sorry I dont have the database so I cannot test it for typeerrors or logical errors. I f you get in troubels feel free to ask again or send an email.