I have a UserForm with 6 text-boxes, which the user can fill (or not) 2, 3, 4, or 6 or whatever he/she wants. These are called:
eb1, eb2, eb3, ...
At the end of the form there is also an 'accept' buttom, and when the user clicks it a text should appear as follows:
eb1, eb2, eb3, eb4...
Of course one of the problems is that if the user only fills 3 boxes, the result might be:
eb1, eb2, eb3, , ,
How can I do this? I guess I need something like a variable array and a for loop, but no idea how to do that, I am pretty new to VBA.
Thanks a lot.
Try below.
Create user form with 6 textboxes & name them as eb1, eb2, eb3, eb4, eb5 & eb6. Then add command button & paste below code to it. Then run the userform & enter some values to text boxes.
Click on the command button to see the msgbox.
Private Sub CommandButton1_Click()
dim str as string
dim a as integer
'Check for text box name & loop through textboxes
for a=1 to 6 'No of text boxes have.(remember that your text boxes should have names like eb1
'find out if there is any blank textboxes & avoid them
if Controls("eb" & a).Value <> "" then
'Correction for front , if eb1 is empty
if str="" then
str=Controls("eb" & a).Value
else
str=str & " , " & Controls("eb" & a).Value
end if
end if
next
msgbox str
end Sub
Final messagebox will give you the result you want. (If I understood your question clearly). I have assumed that you don't need to show anything regards empty textboxes.
Keashan
Related
I need some help making this code shorter. I have a lot of textboxes with times in my form which I want to copy to my Excel file. Some of the boxes needed to be 15min less in Excel sheet and I want to loop through these textboxes and and copy the result into the correct cell. My code works but I want to make it for more textboxes at once.
Dim d1re1 As Date = TextBox5.Text
TextBox5.Text = d1re1.ToLongTimeString()
Dim d1nre1 As Date = d1re1.AddMinutes(-15)
xlsp1.Cells(7, 100) = d1nre1.ToLongTimeString
Dim d1re2 As Date = TextBox7.Text
TextBox7.Text = d1re2.ToLongTimeString()
Dim d1nre2 As Date = d1re2.AddMinutes(-15)
xlsp1.Cells(7, 102) = d1nre2.ToLongTimeString
The idea here that the lines somehow doing the same but with different parameters.
Here functions come in the picture. You can just create generic method call it for ex : SetDateToFieldscand takes the parameters : the source textbox and which cell it needs to update.
Like this you just need only to call the method with different params.
I'm relatively new to VBA. I have a program I'm writing where the user is given the option to change their input from a 2 dimensional array in another user form.
The first user form, UserForm1, allows the user to input the information from text fields and saves it to the respective array row, i, when pressing a Save command button.
When the user presses an OK command button, the user is asked if they want to add another set of data. If they say no, they are asked if they want to change data. If they say yes, then another user form, UserForm2, is opened.
The code for UserForm1 is similar to the code below:
Public MyArray as Variant, i as Integer
Sub Userform_Initialize()
ReDim MyArray(100,4)
End Sub
Sub SaveButton_click()
MyArray(i, 1) = TextField1.Value
MyArray(i, 2) = TextField2.Value
MyArray(i, 3) = TextField3.Value
MyArray(i, 4) = TextField4.Value
End Sub
Sub OKButton_click()
If msgbox("Do you want to add more data?", vbYesNo) = vbNo Then
If msgbox("Do you have corrections to be made?",vbYesNo) = vbYes Then
Load UserForm2
UserForm2.Show
Else: Exit Sub
End If
Else: i = i + 1
Exit Sub
End If
End Sub
In UserForm2, the user chooses the row number, i, from a combo box. When the row number is selected, the array information is automatically populated in text fields from UserForm1.
When the user presses the Save command button, it should pass the information from the text fields and write it to the respective row.
The code for UserForm2 is similar to the code below:
Public j as integer
Sub Userform_Initialize()
For j = 1 to UserForm1.i
ComboBox1.AddItem (j)
Next
End Sub
Sub SaveButton_click()
UserForm1.MyArray(ComboBox1.Value, 1) = TextField1.Value
UserForm1.MyArray(ComboBox1.Value, 2) = TextField2.Value
UserForm1.MyArray(ComboBox1.Value, 3) = TextField3.Value
UserForm1.MyArray(ComboBox1.Value, 4) = TextField4.Value
End Sub
Stepping through the code, the values from MyArray should be properly referenced, and I can see the values initially saved from UserForm1. However, the values are not changing as I step to the next line.
Does anyone have a solution for my problem? Thank you in advance for your help!
I believe I found my solution. I had to declare the array in the module containing the code to start the program as a public variable. After I did that and modified the code, the values were written properly to the array.
If anyone has other solutions, though, I would like to know. I'm not that experienced with VBA, so I want to hear other solutions.
I have created a forum in vb6 and made connection with a database in access,everything went perfect.
my problem is in my form there is 2 combobox one to select Number and other to get me other numbers (watch the video to understand )
anyway the first combo is working and the second combo is working too but after selecting different number from the first combo i don't get anything in the second combo.anyway i know i just miss something in the code something very stupid
i have uploaded a video so you can see my problem, thanks in advance.
Private Sub Form_Load()
liaison
Do Until rspatient.EOF
Me.npa.AddItem rspatient.Fields(0)
rspatient.MoveNext
Loop
End Sub
Private Sub npa_Click()
rspatient.MoveFirst
Dim cr As String
cr = "npation ='" & npa & "'"
rspatient.Find cr
nom = rspatient.Fields(1)
prenom = rspatient.Fields(3)
rshospita.MoveFirst
nh.Clear
While rshospita.EOF = False
If UCase(rshospita.Fields(14)) = UCase(npa) Then
nh.AddItem rshospita.Fields(0)
End If
rshospita.MoveNext
Wend
End Sub
video for more detail :
https://www.youtube.com/watch?v=Tidm18_tvp0
The simplest possible reason for your problem is that you don't have any hospital records associated with your second patient. Check that first.
However, your code is also a bit convoluted. A simpler way to do what you want is to use Filter instead of Find. Filter restricts your recordset to only those records which match the filter, so you can simply iterate the filtered recordset the same way you do the whole one (rspatient). Something like this:
Private Sub npa_Click()
With rshospita
.Filter = ".Fields(14) = '" & npa & "'"
.MoveFirst
nh.Clear
Do Unitl .EOF
nh.AddItem .Fields(0)
.MoveNext
End With
End Sub
I have a system within my html holiday booking app which prints out an array of holiday requests which look like so.
For i = 0 To Ubound(arrHolsAccept) Step 1
response.Write("<form action=""AdminGUI.asp"" method=""post"">")
Response.write("<tr>")
Response.write("<td>"& "<input type=""hidden"" name=""HolidayID"" value=" &arrHolsAccept(i,0)& " " & "</td>")
Response.write("<td>"& arrHolsAccept(i,1) &"</td>")
Response.write("<td>"& arrHolsAccept(i,2) &"</td>")
Response.write("<td>"& arrHolsAccept(i,3) &"</td>")
Response.write("<td>"& arrHolsAccept(i,4) &"</td>")
Response.write("<td><input type=""submit"" name=""accepthol"" value=""Accept""/><td/>")
Response.write("<td><input type=""submit"" name=""declinehol"" value=""Decline""/><td/>")
Response.write("</tr>")
response.write("<form/>")
Next
the Issue I am having is that the accept and decline buttons within each array item set that is printed out need to pass a value to the database. which is the holiday ID arrHolsAccept(i,0) how can I get these submit and decline buttons to pass this value to the database connection element?
so far my connection element of the system looks as follows
objDBCommand.Parameters.Append objDBCommand.CreateParameter("#HolidayRef", adVarChar, adParamInput,200)
objDBCommand.Parameters.Append objDBCommand.CreateParameter("#EmployeeID", adVarChar, adParamInput,200)
objDBCommand.Parameters.Append objDBCommand.CreateParameter("#jobroleofstaff", adVarChar, adParamInput,200)
objDBCommand("#HolidayRef") = Request.Form("HolidayID")
objDBCommand("#EmployeeID") = Session("userID")
objDBCommand("#jobroleofstaff") = Session("JobroleID")
the submit and decline buttons run this section of code and are supposed to pass it a value. as the array builds the holidays it builds multiple of these submit and decline buttons and I do not know how to differentiate the buttons and then assign the correct holiday ID to them, I have tried multiple things so far and I can't seem to get it to work.
You are already passing the HolidayID / arrHolsAccept(i,0) as a hidden field on this line:
Response.write("<td>"& "<input type=""hidden"" name=""HolidayID"" value=" &arrHolsAccept(i,0)& " " & "</td>")
And you are passing that value to the database stored procedure on this line already:
objDBCommand("#HolidayRef") = Request.Form("HolidayID")
I think the problem you are having is possibly because you are not closing your form. You have an error in your code and need to change this line:
response.write("<form/>")
To:
response.write("</form>")
Put the values of arrHolsAccept(i,1) as the value in a hidden field, comma-separated, then parse it on the server after the form is submitted.
I have an Access database that I want to loop through certain textboxes in order to do a calculation and display the answer in a seperate textbox. When I attempt to loop it loops through every control on my form (with the Form.Controls) method. I would like to only loop through 4 specific textboxes (txtbx1, txtbx2, txtbx3, txtbx4) when my button is clicked.
Explanation...
TextBox_A contains a Number
Upon button click take the number from TextBox_A, Multiply by 2800, then Divide by 12
Display the answer to the calculation in txtbx1.
I would do this for each of the 4 textboxes named above. Then have a "Total" textbox that adds up the total from each textbox (txtbx1, txtbx2, txtbx3, txtbx4). Please help, new to this and at a complete loss.
Every control has got a Tag property. Set the Tag property for textboxes whose values you want to include in your algorithm (say to "INClUDE"). It's free format text, so you can put what you like.
Then write code attached to some form event similar to this:
Dim c As Control
Dim txt As TextBox
For Each c In Me.Controls
'check it's a text box ...
If TypeOf c Is TextBox Then
'see if including ...
Set txt = c
If Len(txt.Tag) > 0 Then
'do something here (I've coloured, to show works)
txt.BackColor = 10
End If
End If
Next c
I've set an extra variable txt to refer to the textbox in question, so that I can get at the TAG property using autocompletion (I believe this is called a narrowing conversion!).
It would be easier if you rename TextBox_A (_B / _C etc) to TextBox_1 / _2 / _3 etc.
And then try this:
Dim i As Integer
For i = 1 To 4
Controls("txtbx" & i) = Val(Controls("TextBox_" & i)) * 2800 / 12
Next
Otherwise, this would work too, but it is not well readable:
= Val(Controls("TextBox_" & Chr(Asc("A") + i - 1))) * 2800 / 12