473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Populate Controls Randomly from Access Database

3
Greetings

I am busy with a program that randomly selects data from a databse and then populates controls with the random data. the user must then select an option that must be stored for printing at the end of the program.

Below I have pasted my code. I am using VB6 and MSAccess Database.

Thanking You in Advance.

Julian

ps. I am a beginner

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim CN As ADODB.Connection
  3. Public RS As ADODB.Recordset
  4. Dim i As Long
  5. Dim strArray(5) As Long
  6. Dim x As Long
  7. Dim z As Long
  8. Dim WordCounter As Long
  9. Dim CurrentWord As Long
  10. Dim SQL As String
  11.  
  12. Private Sub OpenConnection()
  13.  
  14. Set CN = New ADODB.Connection
  15. CN.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\cas.mdb"
  16. CN.Open
  17.  
  18. End Sub
  19.  
  20. Private Sub ReturnRecords()
  21.  
  22. Set RS = New ADODB.Recordset
  23.  
  24. OpenConnection
  25.  
  26. With RS
  27.     .ActiveConnection = CN
  28.     .Source = "SELECT * FROM [caslit]"
  29.     .CursorType = adOpenStatic
  30.     .LockType = adLockOptimistic
  31.     .Open
  32. End With
  33.  
  34. End Sub
  35.  
  36. Private Sub cmdNext_Click()
  37.  
  38. Test    'jumps to sub. to generate the next question
  39.  
  40. lblStatus.Caption = RS.AbsolutePosition & "  of  " & RS.RecordCount
  41.  
  42. End Sub
  43.  
  44. Private Sub Form_Load()
  45.  
  46. ReturnRecords
  47.  
  48. lblDate.Caption = Date
  49.  
  50. lblTime.Caption = Time
  51.  
  52. 'Test
  53.  
  54. Finder
  55.  
  56. lblStatus.Caption = RS.AbsolutePosition & "  of  " & RS.RecordCount
  57.  
  58. End Sub
  59.  
  60. Private Sub Form_Paint()
  61.  
  62. Dim wid As Single
  63. Dim hgt As Single
  64. Dim x As Single
  65. Dim y As Single
  66.  
  67.     wid = Picture1.ScaleWidth
  68.     hgt = Picture1.ScaleHeight
  69.     y = 0
  70.     Do While y < ScaleHeight
  71.         x = 0
  72.         Do While x < ScaleWidth
  73.             PaintPicture Picture1.Picture, _
  74.                 x, y, wid, hgt
  75.             x = x + wid
  76.         Loop
  77.         y = y + hgt
  78.     Loop
  79.  
  80. End Sub
  81.  
  82. Private Sub mnuExit_Click()
  83.  
  84. Set CN = Nothing
  85. Set RS = Nothing
  86.  
  87. Load frmIndex
  88. frmIndex.Show
  89.  
  90. Unload Me
  91.  
  92. End Sub
  93.  
  94. Private Sub Test()
  95.  
  96. Dim RndNum As Long
  97. Dim strCount As Long
  98.  
  99. For i = 0 To 5
  100. If strCount = 1 Then
  101. i = i - 1
  102. strCount = 0
  103. End If
  104. strCount = 0
  105. Randomize
  106. RndNum = Int((5 * Rnd) + 1)
  107.  
  108.     x = 0
  109.     For x = 0 To i
  110.  
  111.         If strArray(x) = RndNum Then
  112.         strCount = 1
  113.  
  114.         End If
  115.  
  116.     Next x
  117.  
  118.     If strCount < 1 Then
  119.         strArray(i) = RndNum
  120.         strCount = 0
  121.  
  122.     End If
  123.  
  124. Next i
  125.  
  126. 'Call Finder
  127.  
  128. End Sub
  129.  
  130. Private Sub Finder()
  131.  
  132. Call Test
  133.  
  134. WordCounter = 5
  135.  
  136. 'create
  137. SQL = "SELECT * FROM caslit WHERE indexid="
  138. CurrentWord = 0
  139. For z = 0 To WordCounter - 1
  140. If z <> WordCounter - 1 Then
  141. SQL = SQL & "'" & strArray(z) & "' OR indexid="
  142. CurrentWord = CurrentWord + 1
  143. ElseIf z = WordCounter - 1 Then
  144. SQL = SQL & "'" & strArray(z) & "'"
  145. End If
  146. Next z
  147.  
  148. lblQuestion.Caption = RS!question
  149. OptA1.Caption = RS!c1
  150. OptA2.Caption = RS!c2
  151. OptA3.Caption = RS!c3
  152. OptA4.Caption = RS!c4
  153.  
  154. End Sub
Mar 1 '07 #1
4 1644
willakawill
1,646 1GB
Hi. This is a lot of code. You have not posted a clear question. Which part of your code is not working? What, specifically, are you stuck on?
Mar 4 '07 #2
tjbvo
3
Hi. This is a lot of code. You have not posted a clear question. Which part of your code is not working? What, specifically, are you stuck on?
[quote=tjbvo] Hi. Apologies.

The code "Private Sub Test()" is to do the random selection, and also to eliminate any duplicates.

The code "Private Sub Finder()" is to randomly select the data from the database.

However, when the cmdNext button is clicked, nothing happens. It is supposed to display the next random data in the lblQuestion, optA1, optA2, optA3 and optA4 fields/controls.

These are the only "Subs" that are giving me problems.

Thanking You for your help in Advance.
Julian

[quote]
Mar 4 '07 #3
willakawill
1,646 1GB
Hi Julian
So it looks like your problem is in this snippet:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Test()
  2.  
  3.     Dim RndNum As Long
  4.     Dim strCount As Long
  5.  
  6.     For i = 0 To 5
  7.  
  8.         If strCount = 1 Then
  9.             i = i - 1
  10.             strCount = 0
  11.         End If
  12.  
  13.         strCount = 0
  14.         Randomize
  15.         RndNum = Int((5 * Rnd) + 1)
  16.  
  17.         For x = 0 To i
  18.  
  19.             If strArray(x) = RndNum Then
  20.                 strCount = 1
  21.             End If
  22.  
  23.         Next x
  24.  
  25.         If strCount < 1 Then
  26.             strArray(i) = RndNum
  27.             strCount = 0
  28.         End If
  29.  
  30.     Next i
  31.  
  32. End Sub
Copy this code and put comments all the way through saying what the different lines are for and what you want to do.
Mar 4 '07 #4
tjbvo
3
Hi Julian
So it looks like your problem is in this snippet:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Test()
  2.  
  3.     Dim RndNum As Long
  4.     Dim strCount As Long
  5.  
  6.     For i = 0 To 5
  7.  
  8.         If strCount = 1 Then
  9.             i = i - 1
  10.             strCount = 0
  11.         End If
  12.  
  13.         strCount = 0
  14.         Randomize
  15.         RndNum = Int((5 * Rnd) + 1)
  16.  
  17.         For x = 0 To i
  18.  
  19.             If strArray(x) = RndNum Then
  20.                 strCount = 1
  21.             End If
  22.  
  23.         Next x
  24.  
  25.         If strCount < 1 Then
  26.             strArray(i) = RndNum
  27.             strCount = 0
  28.         End If
  29.  
  30.     Next i
  31.  
  32. End Sub
Copy this code and put comments all the way through saying what the different lines are for and what you want to do.
[quote=tjbvo]
Greetings

I have placed comments in both subs.

Hope this will be of help.


Private Sub Test()

Dim RndNum As Long ' declare variable
Dim strCount As Long ' declare variable

For i = 0 To 5 ' setup count
If strCount = 1 Then ' setup initial start point
i = i - 1 ' reduce start point by one
strCount = 0 ' setup actual start point in database
End If ' end setup
strCount = 0 ' confirm database start point
Randomize ' invoke random function
RndNum = Int((5 * Rnd) + 1) ' setup random function for return of data

x = 0 ' setup an array
For x = 0 To i ' declare array values

If strArray(x) = RndNum Then ' start of array
strCount = 1 ' confirm that array has a value

End If

Next x ' next array value

If strCount < 1 Then ' find value of array
strArray(i) = RndNum ' confirm that array value in not duplicate
strCount = 0 ' if array is duplicate set value to 0. force new random array value

End If

Next i ' next non duplicate array value

'Call Finder ' here i have commented out the next sub as i was having problems.
' this call statement will not be active as i was just testing the
' sub by calling the next sub procedure directly from here.

End Sub

Private Sub Finder()

Call Test ' calling the previous sub procedure

WordCounter = 5 ' setting of limit of variable

'create
SQL = "SELECT * FROM caslit WHERE indexid="
CurrentWord = 0 ' setting of initial value
For z = 0 To WordCounter - 1 ' declaring value of variable
If z <> WordCounter - 1 Then ' establishing value of variable
SQL = SQL & "'" & strArray(z) & "' OR indexid="
CurrentWord = CurrentWord + 1 ' setting value of variable
ElseIf z = WordCounter - 1 Then ' if value of variable is nothing then get next value
SQL = SQL & "'" & strArray(z) & "'"
End If
Next z ' return next value from database

lblQuestion.Caption = RS!question ' display value returned from database
OptA1.Caption = RS!c1 ' display value returned from database
OptA2.Caption = RS!c2 ' display value returned from database
OptA3.Caption = RS!c3 ' display value returned from database
OptA4.Caption = RS!c4 ' display value returned from database

End Sub

ps. In the sub "Finder" must the "next z" be before or after the display controls ?

Thanks for your assistance in advance

Julian
[quote]
Mar 10 '07 #5

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

Similar topics

3
by: Bob Bedford | last post by:
I've a site where companies add their article. I'de like to provide a "lasts articles" table. By this, I'll show last articles inserted. But I won't always the same articles at any refresh....
1
by: mike | last post by:
I'm fairly green with javascript, and have been looking everywhere for an example of how to load data from a mysql db into javascript arrays. For a little background on what I'm trying to do, any...
4
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
1
by: HalifaxPinball | last post by:
I want to collect data about some music in a database. Few tricky things that I have questions about... I'd like to start to populate the database with some info from a website. The web site is...
2
by: Mark | last post by:
I am attempting to populate several textbox controls from VBA code. With each attempt, I get the following error: "The macro or function set to the BeforeUpdate or ValidationRule property for...
3
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to...
2
by: Phillip | last post by:
In order to maintain consistency thru a project. Our development team is getting ready to start a new project in converting all the foxpro apps to VB/SQL Server. We don't have the luxury of...
4
by: Terry Olsen | last post by:
I have an access database with a table that contains two columns: Computer_Name,User_ID The table contains 600 computer names. I need to populate the User_ID column with data from an SQL...
3
by: =?Utf-8?B?c29uaWNt?= | last post by:
Hi, What is the best way of populating a repeater control from a SQL Database using a base class. I am trying to impliment a base class that can be used across future websites and will populate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.