I am having a issue with multiline stings in a userform.
When a user selects a option, the code checks if the selected answer matches the correct answer and then shows if right or wrong. But in either case the code says it is wrong.
Example of a option is:
If you see the string, brush it off sideways
Place icepack/cold flannel to reduce swelling
Elevate area to reduce bloodflow
Private Sub OptionButton1_Click()
rowNum = Selection.Row - Selection.ListObject.Range.Row
DeclareVars
Column = examtable.ListColumns("Right ans").DataBodyRange(rowNum)
CorrectAns = examtable.ListColumns("Right ans").DataBodyRange(rowNum).Offset(0, Column)
RightWrong.Visible = True
If OptionButton1.Caption = CorrectAns Then
RightWrong.BackColor = &HFF00&
RightWrong.Caption = "Right"
Else
RightWrong.BackColor = &HFF&
RightWrong.Caption = "Wrong"
End If
End Sub
What i am expecting is that if correct, shows right, or wrong if incorrect
If I understand correctly you have multiple option buttons below one another? Option button one will always have the same caption. If it is selected it can become true (indicated by the black dot), but the caption will not change.
Lets say it looks like this
two option buttons
Then the upper is called OptionButton1, the lower OptionButton2.
You can check
If OptionButton1 then
RightWrong.BackColor = &HFF00&
RightWrong.Caption = "Right"
Else
RightWrong.BackColor = &HFF&
RightWrong.Caption = "Wrong"
End if
You could use a combobox (these are the drop down menus).
You could populate it with an array of the strings you want to test and ask if the answers concur.
When loading the user form use
Private Sub UserForm_Activate()
ComboBox1.list = Array("brush it off sideways", "Place icepack/cold flannel to reduce swelling", "Elevate area to reduce bloodflow")
End Sub
this will look as followed
The user form with the list open
Then you can say combobox.value = CorrectAns
Related
I have an array which contains customer's names as arrNama variable
I use a code to filter as I type which I've found in the internet (I am sorry I don't remember the link), something like this :
Sub cbNama_Populate(Optional fltr As String)
cbNama.List = Filter(arrNama, fltr, , vbTextCompare)
End Sub
Private Sub cbNama_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Call cbNama_Populate(cbNama.Text)
If cbNama.ListCount = 1 Then cbNama.ListIndex = 0 Else cbNama.DropDown
End Sub
The If cbNama.ListCount = 1 Then cbNama.ListIndex = 0 Else cbNama.DropDown line is my modification, the original I found in the internet is just cbNama.DropDown.
I modified that line, because I thought with that IF conditon, it won't show the drop-down list if the list count of the combo box is only one. Yet it still show the drop-down list with that one item only, just like as if in the original code which has no IF condition.
Below is the example of what I mean :
At the time the combobox has only one item, the cbNama.ListIndex = 0 trigger the cbNama_Click sub. But the combobox still show the drop-down list, highlighted with blue on that one item.
My question:
How to prevent it showing the drop-down list when there is only one item?
Any kind of response would be greatly appreciated.
Thank you in advanced.
For the time being, I use cbNama.Enabled = False, this prevent the dropdown shows if there is one item only.
I have a form in VBA where I want a combo box (cboIncluded) to change its dropdown values according to what's chosen in a different combo box(cboSelectParty).
It provides choices correctly the first time I choose something from cboSelectParty, and if I choose again from either my If or elseif.
My problem is, if I have chosen from either the if or elseif (Fun climb party or climbing party), if won't go back to giving me correct options if I choose anything else.
Private Sub cboSelectParty_AfterUpdate()
cboIncluded.Value = ""
If cboSelectParty.Value = "Fun Climb Party" Then
cboIncluded.List = Sheets("PartyImport").Range("N3:N4").Value
ElseIf cboSelectParty.Value = "Climbing Party" Then
cboIncluded.List = Sheets("PartyImport").Range("O3:O6").Value
Else
cboIncluded.Value = Application.WorksheetFunction.VLookup(cboSelectParty.Value,
Sheets("PartyImport").Range("E2:H37"), 4, False)
End If
End Sub
The current function I use to collect text InputBox can't accept more than 255 characters apparently, and I need to be able to collect more than that? Is there a parameter or different function I can use to increase this limit?
To be pedantic, the Inputbox will let you type up to 255 characters, but it will only return 254 characters.
Beyond that, yes, you'll need to create a simple form with a textbox. Then just make a little "helper function" something like:
Function getBigInput(prompt As String) As String
frmBigInputBox.Caption = prompt
frmBigInputBox.Show
getBigInput = frmBigInputBox.txtStuff.Text
End Function
or something like that...
Thanks BradC for the info that. My final code was roughly as follows, I have a button that calls the form that I created and positions it a bit as I was having some issues with the form being in the wrong spot the everytime after the first time I used.
Sub InsertNotesAttempt()
NoteEntryForm.Show
With NoteEntryForm
.Top = 125
.Left = 125
End With
End Sub
The userform was a TextBox and two CommandButtons(Cancel and Ok). The code for the buttons was as follows:
Private Sub CancelButton_Click()
Unload NoteEntryForm
End Sub
Private Sub OkButton_Click()
Dim UserNotes As String
UserNotes = NotesInput.Text
Application.ScreenUpdating = False
If UserNotes = "" Then
NoteEntryForm.Hide
Exit Sub
End If
Worksheets("Notes").ListObjects("Notes").ListRows.Add (1)
Worksheets("Notes").Range("Notes").Cells(1, 1) = Date
Worksheets("Notes").Range("Notes").Cells(1, 2) = UserNotes
Worksheets("Notes").Range("Notes").Cells(1, 2).WrapText = True
' Crap fix to get the wrap to work. I noticed that after I inserted another row the previous rows
' word wrap property would kick in. So I just add in and delete a row to force that behaviour.
Worksheets("Notes").ListObjects("Notes").ListRows.Add (1)
Worksheets("Notes").Range("Notes").Item(1).Delete
NotesInput.Text = vbNullString
NotesInput.SetFocus ' Retains focus on text entry box instead of command button.
NoteEntryForm.Hide
Application.ScreenUpdating = True
End Sub
I don't have enough rep to comment, but in the sub form_load for the helper you can add:
me.AutoCenter = True
Outside of that form, you can do it like this:
NoteEntryForm.Show
Forms("NoteEntryForm").AutoCenter = True
My Access forms get all confused when I go from my two extra monitors at work to my one extra monitor at home, and are sometimes lost in the corner. This AutoCenter has made it into the form properties of every one of my forms.
I am working on moving a form over to Excel. Due to the style of form I have to replicate the form with no changes.
On the form there are two check boxes, one for Full and one for partial.
I have the code spit out a warning box if both are selected. I think it would be easier to make it so that if one is checked the other will automatically uncheck.
' ERROR CHECK FULL/PARTIAL
If [F8] = True And [F9] = True Then
MsgBox ("FAI can not be both a Full and Partial")
ELSE
'actual code in middle.
END IF
There are two ways to do this. With Radio buttons, or checking if the other box is checked.
With radio buttons is simpler if one of them must be selected no matter what. You can enclose radio buttons on different frames to "group" them together as in the screenshot below. You can see that one on each frame is selected and you can only select one of the two on each frame.
If you really want to use a Checkbox, you can use the code below.
Private Sub CheckBox1_Click()
If Me.CheckBox1 = True Then
If Me.CheckBox2 = True Then Me.CheckBox2 = False
End If
End Sub
Private Sub CheckBox2_Click()
If Me.CheckBox2 = True Then
If Me.CheckBox1 = True Then Me.CheckBox1 = False
End If
End Sub
The first If checks if this is a click to Check the box, if it is, then it will check if the second box is already checked, if it is, it will un-check it.
This is assuming you are creating a UserForm, and these are not CheckBoxes on an Excel Sheet. The implementation would be the same, but the code will be slightly different.
Something like this:
Private Sub opt01_Click()
If opt01.Value = True Then
opt01.Value = False
Else
opt01.Value = True
End If
It would be suitable for my formDesign, instead of looking for and importing some similar small picture.
Because an OptionButton's value is always true when the Click event runs, you will need to store its desired value in a variable. Here is an example using a Form Control Option Button within a sheet named Option Button 1 on sheet 1. This method is stored in a module.
Private optionClicked As Boolean
Sub OptionButton1_Click()
Dim o As OptionButton
Set o = Sheets(1).Shapes("Option Button 1").OLEFormat.Object
o.Value = Not optionClicked
optionClicked = Not optionClicked
End Sub
Note however, that using this method, that clicking on any option button will make all other option buttons false. So to use multiple option buttons as check boxes, you will need to store the correct value of the option buttons (probably in an array)... and correct the values every time any option button is clicked.
Are you sure you can't use check boxes?