I am trying to get 6 separate input numbers from the user for my program using one textbox - vb6

I have been trying to use a textbox in VB6 to acquire 6 different random numbers chosen by the user to use in part of my program. I have tried many different approaches in different areas of the code. Best I have gotten so far is in the keypress section. I set up an array to store the 6 entries buy fail to be able to get past storing one number in the first element. I'm using a for next loop to cycle through the elements but the textbox.setfocus will not work properly in the loop. I clear the box then setfocus after I assign the number to an element in the array. The only other way I'm thinking this would work is to hide the single box and show 6 hidden ones and then they can tab or I can hide each one again as they fill up.
Here's a snip of that code so one should get an idea what I'm trying to do here.
Private Sub Inbox_Keypress(KeyAscii As Integer)
Select Case KeyAscii ' Determine keypress
Case vbKey0 To vbKey9 'Only the numbers
Case vbKeyBack, vbKeyClear, vbKeyDelete ' Accept these keys
Case vbKeyLeft, vbKeyRight ' Arrow keys
Case 13 ' Enter
If GenOpt(5).Value = True Then
For c = 1 To 6
Gen6_user(c) = Inbox.Text
'Select Case KeyAscii
' Case 13
Talkbox.Caption = "Please enter the next user number for Generator 6"
'Case Else
' MsgBox Msg, style, title
'End Select
If c = 6 Then Exit For
Do
Inbox.Text = ""
Inbox.SetFocus
Loop Until KeyAscii = 13
Next
For c = 1 To 6
Msg6.Print Gen6_user(c)
Next
Else
User_number = Inbox.Text ' Assign any final value to User_number variabl)
Generate.SetFocus
'Generate_Click ' Call generate function
End If
Any help would be appreciated
Journey

I am at work and I don't have VB6 handy here... but, this should work (maybe with a few little tweaks, I am sure you can figure those out):
Dim x As Integer = 1
Dim myArray(6) As Integer
Private Sub TextBox1_KeyUp(KeyCode As Integer)
If KeyCode = 13 Then
myArray(x) = TextBox1.Text
x = x + 1
TextBox1.Text = ""
End If
If x = 6 Then
Debug.Print("Here you check your numbers?")
End If
End Sub

Related

Looping error, too many records added

Ive been trying to write Access VBA code to automate the addition of replicates for germination tests.
Basically I have a form where I enter the total number of Reps (NoofReps) and the number of seeds per rep (RepSize) (e.g. 50 seeds). For each record added I want it to automatically add a record for each rep and automatically calc the Rep Number (i.e if i have 4 reps then it should add 4 records, numbered 1-4 reps) as well as the RepSize (e.g 50).
I have been trying out various loops based on information from this forum and other but am still getting errors with the number of records that it generates. I have tried both the "Do while" and "Do Until" but get the same result below either way.
Could someone please let me know where I am going wrong?...If i want 2 reps then it adds 2, If i want 3 then its 246, and if i want 4 it adds >30,000!!!
For the purposes of trying to fix the code I have started to type the number of reps manually into the code in the iNoofReps so that I know the error is in the code and not from the form.
Private Sub CmdAddReps3_Click()
Dim iRepNo As Integer ' stores the current value in the series
'Open the table
Set db = CurrentDb()
Set rstGReps = db.OpenRecordset("tblGReplicates")
' Initialise the variables
iRepNo = 1
iNoofReps = 3 'iNoofReps = Me.txtNoofReps
' Add the records using a loop
rstGReps.movefirst
Do 'Until rstGReps("RepNo") = (iNoofReps + 1) ' always want to include at least 1 repNo
rstGReps.AddNew
rstGReps("GTestID") = Me.GTestID
rstGReps("RepNo") = iRepNo
rstGReps("NoofSeed") = Me.txtNoOfSeeds
' Calculate the next RepNo value in the loop
iRepNo = iRepNo + 1
rstGReps.Update
rstGReps.moveNext
Loop Until rstGReps("RepNo") = (iNoofReps) + 1 ' so that the loop includes the final repNo.
MsgBox "Finished Looping"
rstGReps.Close
Set rstGReps = Nothing
Set db = Nothing
End Sub
Any help would be appreciated!!!
Well, you're moving next here: rstGReps.moveNext, and then you're comparing rstGReps("RepNo") = (iNoofReps) + 1 after moving next, thus being on an empty record, thus always equating to false.
Loop Until iRepNo = (iNoofReps) + 1 should fix it, then you're no longer referring to the recordset, which has already been set to the next record by the time you're referring to it.
You could also fix it by just eliminating this line:
rstGReps.moveNext
Since rstGReps.AddNew already moves the recordset to a new blank record, moving it forward after adding the record doesn't make much sense. If you remove it, you might want to remove the + 1 in Loop Until rstGReps("RepNo") = (iNoofReps) + 1

Access-VBA for loops

I am quite new to VB coding but I do understand some of the basics. I want to create a loop that checks the value within 8 text boxes and enabled a tick box next to the respected text box if it not blank. I am not quite sure on the syntax. I have 8 text boxes called Textbox1 : Textbox8 and 8 checkboxes with names checkbox1-8. This is the code I have already but I just need a bit of help with the syntax.
For i = 1 To 8
If Textbox(i) = "" Then
checkbox(i).enabled = false
else
checkbox(i).enabled = true
End If
Next
Something like:
For i = 1 To 8
Me.Controls("checkbox" & i).Enabled = (Nz(Me.Controls("Textbox" & i).Value) <> "")
Next

Displaying the entire number in vb6

So I have a program coded in Visual Basic 6 that is essentially Cookie Clicker. when it gets into higher numbers though, it displays "1E+16" and so on. I realize this is what is supposed to happen but is there any way to actually display the full number?
Edit:
The number is being stored as a double.
Private Sub timerDisplay_Timer()
lblClicks.Caption = dblClicks
End Sub
is displaying the number and
Private Sub cmdClick_Click()
If currentItems = 0 Then
dblClicks = dblClicks + 1
Else
dblClicks = (dblClicks + (currentItems * 2))
End If
End Sub
is incresing the number.
When the number is "100000000000000" (14 zeros), the label displays the number. but when the number is "1000000000000000" (15 zeros) it'll display 1E+15. i want is to actually display the number and not the "xE+y" format.
Maybe something like this will work for you
dblClicks = 1E+15
Me.lblClicks.Caption = FormatNumber(dblClicks, vbFalse, vbFalse)
Me.lblClicks.Caption = Replace(Me.lblClicks.Caption, ",", vbNullString)

Non repeating positive random numbers, over a given range

am generating a game using vb 6.0, at one face i need to generate non repeating random numbers between 1 and 100. am using the following code for generating the random numbers
dim n(10) as integer
for i=0 to 9
n(i)=round(rnd*100)
next i
the code is within a for loop hence it generate 10 Nos. randomly, but it includes repeating numbers and also '0', is their any suggestion to avoid repeating numbers and '0'
then output of my code is:
n()={42,14,10,22,5,42,12,0,59,72}
the numbers 42 is appear two times in the array and 0 cannot be avoided
thanks in advance
Here is a simple technique
Dim n(10) As Integer
Dim choice(100) As Integer
' Create a list of possible numbers
For i = 0 To 100
choice(i) = i
Next
' Populate the array with unique numbers
For i = 1 To 10
lastix = 101 - i
' Find one that has not been selected
ix = Round(Rnd * lastix)
' Assign it
n(i) = choice(ix)
' Replace with one that has not been used
choice(ix) = choice(lastix)
Next
You can use the same technique for shuffling cards.
To avoid 0, multiply by 99 and add 1. To avoid duplicates, keep track of what you generated and retry if you get a duplicate. (Since you need only a few numbers. If you need many, shuffle an array of all possible outcomes and take the initial members.)
the solution below is not the fastest, but makes up by that for being easy ...
it uses a hidden listbox control which contains the required values and retreives a random one every time
the values in this example are just the squared numbers of the index
run the project and click the command button to see what happens. it should show messageboxes with the square numbers in random order
'1 form with:
'1 listbox control : name=List1
'1 command button : name=Command1
Option Explicit
Private Sub Command1_Click()
Dim intIndex As Integer
'generate a new random sequence every time
Randomize Timer
'loop through the list and retreive 1 random value at a time
With List1
Do While .ListCount > 0
intIndex = Int(Rnd * .ListCount)
MsgBox .List(intIndex)
'remove the used item from the list
.RemoveItem intIndex
Loop
End With 'List1
End Sub
Private Sub Form_Load()
Dim intIndex As Integer
'hide listbox
List1.Visible = False
'fill listbox with values (squares)
List1.Clear
For intIndex = 1 To 10
List1.AddItem CStr(intIndex * intIndex)
Next intIndex
End Sub
btw i am just using 10 numbers so you dont have to click through 100 messageboxes :)

Search WebList for particular item count from Excel sheet

I need a help in VBScript or either in QTP for the below case.
For example:
I have nearly 40 items in the weblist. I have only one item in the Excel sheet that is one among the 40 in the weblist. If I run the script, the one in the Excel should be select in the weblist. How do I perform this? I tried many scenarios, but couldn't get it to work.
Below are some of the sample pieces of code I tried in QTP:
ocount=Browser("name:=brw").Page("title:=brw").WebList("htmlid:=tabContainerBrandSite_123&rtyoh").GetROProperty("items count")
msgbox ocount
var7=mySheet2.Cells(2,"C")
For k=2 to ocount
ocount2=Browser("name:=brw").Page("title:=brw").WebList("html id:=tabContainerBrandSite_123&rtyoh").GetItem(k)
msgbox ocount2
merchantName = DataTable("Merchant_Name","Global") 'an example if value is saved in global sheet
items_count = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetROProperty("Items Count") 'This will get all the items from your weblist.
i = 1
Do
webListName = Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").GetItem(i)
'this will get first value from the web list
If merchantName = webListName Then 'comparing first value from your value from global sheet
Browser("Sarit").Page("Sarit_2").WebList("txtVendorCode").Select(i) 'selects that value
Exit do 'because it has found your only value from the local sheet, it exits
else
i = i + 1
End If
Loop While i <= items_count

Resources