VBA Change value in textbox based on whether other textboxes are empty - vba

Struggling with some code here, looking for help. I am trying to figure out the correct syntax for updating a textbox value based on whether another textbox has a value or not.
Breakdown
Textbox1 name = arisk_box
Textbox2 name = adjust_box3
Textbox3 name = rr_box
This is what I have so far:
Private Sub arisk_box_Change()
If arisk_box.Text <> "" And adjust_box3.Text = "" Then
rr_box.Value = arisk_box.Value
ElseIf arisk_box.Text <> "" And adjust_box3.Text <> "" Then
rr_box.Value = adjust_box3.Value
End If
End Sub
*If arisk_box has a *VALUE* and adjust_box3 *DOES NOT* then rr_box = the value from arisk_box
Elseif arisk_box AND adjust_box3 both have a *VALUE* then rr_box = the value from adjust_box3

The simplest way it to use 2 separate macros, one for each textbox change event. But it may not work as smoothly as you want it to. I sure someone will have a more advance method.
Private Sub adjust_box3_Change()
If arisk_box <> "" And adjust_box3 = "" Then rr_box = arisk_box
End Sub
Private Sub arisk_box_Change()
If arisk_box <> "" And adjust_box3 <> "" Then rr_box = adjust_box3
End Sub

Related

Else Box keeps activating but the If solutions are satisfied

Private Sub Dutybox_Change()
Dim Val As String
ThisWorkbook.Worksheets("User Dashboard").Range("L17").Value = Me.Dutybox.Value
Calculate
If Me.ComboBox18.RowSource <> "" And Me.RankCombo.Value <> "" And Me.Exambox.Value <> "" And Me.Dutybox.Value <> "" Then
Me.ComboBox18.RowSource = "=Rate"
Else
MsgBox "Please Select all the above values"
End If
End Sub
So the IF parts are all filled, and it should fill ComboBox18 with =Rate (a named range) but it keeps popping up the Msgbox
Item 1
Private Sub ComboBox18_Change()
Dim Val As String
ThisWorkbook.Worksheets("User Dashboard").Range("L18").Value = Me.ComboBox18.Value
End Sub
Item 2
Private Sub RankCombo_Change()
ThisWorkbook.Worksheets("User Dashboard").Range("L15").Value = Me.RankCombo.Value
End Sub
Item 3
Private Sub Exambox_Change()
Dim Val As String
ThisWorkbook.Worksheets("User Dashboard").Range("L16").Value = Me.Exambox.Value
End Sub
Code to push range to ComboBox18
Private Sub ComboBox18_Intialize()
MsgBox "combo box"
Me.ComboBox18.List = Application.Transpose(Names("Rate").RefersToRange.Value)
End Sub
I can't figure out why Msgbox is populating when the values are satisified.
Thank you in advance
One way to debug this program is to verify the output in Immediate window,
Please break your code at
If Me.ComboBox18.RowSource <> "" And Me.RankCombo.Value <> "" And Me.Exambox.Value <> "" And Me.Dutybox.Value <> "" Then
On immediate window type the following and press enter to verify the conditions
?Me.ComboBox18.RowSource <> ""
?Me.RankCombo.Value <> ""
?Me.Exambox.Value <> ""
?Me.Dutybox.Value <> ""
all the above four condition should return true.
I suspect that your expressions might not properly handle Null values.
Try using Nz() like this: Nz(Me.RankCombo.Value,"") <> ""
You can also add code to check your assessments:
Debug.Print "Combo18", Me.ComboBox18.RowSource <> ""
Debug.Print "Rank", Me.RankCombo.Value <> ""

MS Access - Updating a cell's value

My customer wants to be able to autosum values within a cell. For example, they type in a new value, and this value will be added to the previous one, all within one cell (they don't want another field).
So far, I have 2 forms that will calculate the new value with one they input into the calculator, but it only functions for one cell.
Option Compare Database
Function MyVal(lnIn As Long)
If Len(Me.entry) > 1 Then
Me.entry = Replace(Me.entry, ".", "") & lnIn
'Me.entry = Left(Me.entry, Len(Me.entry)) & "." & Right(Me.entry)
Else
Me.entry = Me.entry & lnIn
End If
End Function
Private Sub calc_Click()
Me.Balance = Me.Balance - (-(Me.entry))
Me.entry = ""
End Sub
Private Sub clear_Click()
Me.entry = ""
End Sub
Private Sub Command16_Click()
Dim MyBalance As Currency
MyBalance = Me.Balance
DoCmd.Close
Forms!frmHours.Form.SetFocus
Forms!frmHours.Form.Balance = MyBalance
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Balance = OpenArgs
End Sub
Is there a way to move the "selected" cell value into the table I have (replacing the old value), do the calculations, and then move the new value back into the selected cell?

Simplify toggle button change BackColor Code VBA

im new in VBA making, so all code below is still working tho but it takes a lot of line of codes. Even it is easier to maintain but if someone can simplify my noob-code to cut some lines and more eye-pleasing?
there are more than 20 toggle buttons in my userform
this is the example of my code, need help for make it simpler
Private Sub tgglC_Result1_Click()
If tgglC_Result1.Value = True Then
tgglC_Result1.BackColor = &HFF00&
tgglNC_Result1.Enabled = False
lblResult1.Caption = Now
lblResult1.Visible = True
Else
tgglC_Result1.BackColor = &H8000000F
tgglNC_Result1.Enabled = True
lblResult1.Visible = False
End If
End Sub
Private Sub tgglC_Result2_Click()
If tgglC_Result2.Value = True Then
tgglC_Result2.BackColor = &HFF00&
tgglNC_Result2.Enabled = False
lblResult2.Caption = Now
lblResult2.Visible = True
Else
tgglC_Result2.BackColor = &H8000000F
tgglNC_Result2.Enabled = True
lblResult2.Visible = False
End If
End Sub
Private Sub tgglC_Result3_Click()
If tgglC_Result3.Value = True Then
tgglC_Result3.BackColor = &HFF00&
tgglNC_Result3.Enabled = False
lblResult3.Caption = Now
lblResult3.Visible = True
Else
tgglC_Result3.BackColor = &H8000000F
tgglNC_Result3.Enabled = True
lblResult3.Visible = False
End If
End Sub
Private Sub tgglC_Result4_Click()
If tgglC_Result4.Value = True Then
tgglC_Result4.BackColor = &HFF00&
tgglNC_Result4.Enabled = False
lblResult4.Caption = Now
lblResult4.Visible = True
Else
tgglC_Result4.BackColor = &H8000000F
tgglNC_Result4.Enabled = True
lblResult4.Visible = False
End If
End Sub
best way should be using a Class
but a more "conventional" way could help you reducing typing burden, too:
define a unique toggle control handling sub
Private Sub tgglC_Result_Click()
Dim NC As Control
With Me
Set NC = .Controls(VBA.Replace(.ActiveControl.Name, "tgglC", "tgglNC")) '<--| set the "counter part" toggle button control of the "Active" control (i.e. the one being currently toggled)
With .ActiveControl
.BackColor = IIf(.Value, &HFF00&, &H8000000F)
NC.Enabled = Not .Value
End With
End With
End Sub
call it from any of your event handler
Private Sub tgglC_Result1_Click()
tgglC_Result_Click
End Sub
Private Sub tgglC_Result2_Click()
tgglC_Result_Click
End Sub
Private Sub tgglC_Result3_Click()
tgglC_Result_Click
End Sub
...
Not really a simplifying solution, but this is what I used when I needed to supply logic to 60+ controls on an Access subform (similar task to yours):
Sub makeCode()
Dim i As Integer
For i = 1 To 4
Debug.Print "Private Sub tgglC_Result" & i & "_Click()"
Debug.Print "tgglC_Result" & i & ".BackColor = &HFF00&"
Debug.Print "tgglNC_Result2.Enabled = False"
Debug.Print "lblResult" & i & ".Caption = Now"
Debug.Print "lblResult" & i & ".Visible = True"
Debug.Print "End Sub"
Debug.Print ""
Next
End Sub
Copy the result from the Immediate window into the code editor. It's easy to change all the subroutines, too: just change the loop body, run it, and replace old code.

How to do the same code for several checkboxes to set the checkbox Value?

I have in my Excel userform several checkboxes (in total 10) which all should be have the same behavior. It means, when I reload the form the value of the checkbox should change, depending on values from the spreadsheet with the actual selected row.
The following code is working properly for one checkbox:
If Sheet1.Cells(iRow, Sheets("aux").Range("B21")) = "X" Then
cbERW.Value = True
ElseIf Sheet1.Cells(iRow, Sheets("aux").Range("B21")) = "?" Then
cbERW.Value = Null
Else
cbERW.Value = False
End If
How can I do this in the easiest way for multiple checkboxes? But for every checkbox the referring row and column could change. For example, the next one look like this (cbHSAW instead of cbERW, Column B22 instead of B21):
If Sheet1.Cells(iRow, Sheets("aux").Range("B22")) = "X" Then
cbHSAW.Value = True
ElseIf Sheet1.Cells(iRow, Sheets("aux").Range("B22")) = "?" Then
cbHSAW.Value = Null
Else
cbHSAW.Value = False
End If
Anybody know how to do this easily?
You can wrap your test statement in a function and pass the range for the specific checkbox. Then set the return result can be as the value for the checkbox. The below needs to be modified for your form and ranges but will give the desired result.
'Function to test the value of the range
Private Function SetCheckBox(ByVal rng As Range)
If rng.Value = "X" Then
SetCheckBox = True
ElseIf rng.Value = "?" Then
SetCheckBox = Null
Else
SetCheckBox = False
End If
End Function
Private Sub UserForm_Initialize()
'Set value for each checkbox by passing its range to the function
CheckBox1.Value = SetCheckBox(Sheets("aux").Range("A1"))
CheckBox2.Value = SetCheckBox(Sheets("aux").Range("A2"))
CheckBox3.Value = SetCheckBox(Sheets("aux").Range("A3"))
CheckBox4.Value = SetCheckBox(Sheets("aux").Range("A4"))
End Sub

UserForm ComboBox

I have a UserForm that has one ComboBox and a TextBox. The TextBox needs to do a vlookup for the value of the ComboBox but ONLY if that value exists in the list, if not, I want nothing to appear in the TextBox so the user can enter new information.
This is how far I have got:
Private Sub TextBox1_Enter()
If cbocolor.Value <> "" Then
Dim evalStr As String
Dim check As Variant
evalStr = WorksheetFunction.VLookup(cbocolor.Value, worksheets("CONTACTS").Range("allcontacts"), 2, False)
check = Evaluate(evalStr)
If VarType(check) = vbError Then
TextBox1.Value = "Enter new info"
Else
var1 = WorksheetFunction.VLookup(cbocolor.Value, Worksheets("CONTACTS").Range("allcontacts"), 2, False)
TextBox1.Value = var1
End If
You should be able to do it all with one line:
Private Sub TextBox1_Enter()
If cbocolor.value <> "" Then
TextBox1.value = WorksheetFunction.IfError(Application.VLookup(cbocolor.value, _
Worksheets("CONTACTS").Range("allcontacts"), 2, False), "Enter New Info")
End If
End Sub