vba Checkbox if else condition in Subform, access - vba

I have a main form with several tab controls with subforms in them. On the first tab or subform I have a "checkbox 1", which shall be:
checked if "textbox 1" is not empty
unchecked if "textbox 1" is empty
The code is directly put into the Class Object of "subform 1", that's why I thought I could use Me.
Here is my code, but I always get error messages :(
Private Sub Ctltextbox_1_AfterUpdate()
If Len(Ctltextbox_1.Value) = 0 Then
Me.checkbox_1.Value = 0
Else
Me.checkbox_1.Value = -1
End If
End Sub
That way I am getting the
Run-time error '2448': You can't assign a value to this object.
On the line that attempts to assign -1 to Me.checkbox_1.Value.

Try this. It is working for me. I moved it to the Ctltextbox_1_Change action. This will call the function whenever it is changed and will catch when you have nothing in the box as well. I also changed your values to True and False.
Please try this and let me know.
Private Sub Ctltextbox_1_Change()
If Len(frmSub.Ctltextbox_1.Value) = 0 Then
frmSub.CheckBox_1.Value = False
Else
frmSub.CheckBox_1.Value = True
End If
End Sub
you can also solve this in one line as mentioned below.
Private Sub Ctltextbox_1_Change()
frmSub.CheckBox_1.Value = IIf((Len(frmSub.Ctltextbox_1.Value) = 0), False, True)
End Sub

Ok, the simple answer is: you can not use the expression Len()= 0 for a not numeric field! So my working code for the subform is now:
Private Sub Textbox1_AfterUpdate()
If IsNull(Textbox1.Value) = False Then
Me.checkbox1.Value = True
Else
Me.checkbox1.Value = False
End If
End Sub
Thank you all for your help! :)

Related

Checking and Unchecking Checkboxes in Access

I have a form in MS Access with multiple checkboxes which I want to use to fill up one textbox. If one of the checkboxes gets unchecked, I want its value to be deleted from the textbox without deleting other values. I'm new at using Access and coding in VBA (been reading ebooks for the past 3 weeks) and although I've tried to do research online it's been difficult for me to find the right code.
This is what I have so far:
First code found
Private Sub cb_click()
If Me.cb1 = True Then
Me.txtComentarios.Value = "INACTIVO;"
Else
Me.txtComentarios.Value = Null
End If
End Sub
Second code found
Private Sub cb2_Click()
If Me.cb2 = -1 Then
Me.[txtComentarios] = [txtComentarios] & "DISCREPANCIA"
Else
Me.[txtComentarios] = ""
End If
Exit Sub
End Sub
Also I would like for the checkboxes to fill the textbox in the same order the chechboxes are displayed.
Ex.
cb1; cb2; cb3
If, cb2 gets unchecked and its value gets deleted, I should have "cb1; cb3" but if I re-check cb2 I should get "cb1; cb2; cb3" again.
I just hope someone could guide me in. Thank you in advance.
Luz
You don't need events for each checkbox. Just create one procedure, which creates full text depending on checkboxes state and puts this text to the textbox. To call this function after each click on checkbox set After Update property of all checkboxes to =MyFunctionToUpdateTextbox instead of [Event Procedure]
Private Function MyFunctionToUpdateTextbox()
Dim strText As String
If Me.cb1 = True Then
strText = strText & "INACTIVO;"
End If
If Me.cb2 = True Then
strText = strText & "DISCREPANCIA;"
End If
If Me.cb3 = True Then
strText = strText & "Text for cb3"
End If
Me.txtComentarios = strText
End Function

Excel VBA Object Helper Function

I have a UserForm where one of my ComboBox's can have different values depending on other variables that the user has selected. Rather than update the contents of a single ComboBox every time the user makes a change, I have created two ComboBox's and simply set the appropriate one as visible. These two ComboBox's have the names "width1_cb" and "width2_cb" respectively.
I have created a helper function to return the currently active ComboBox so that other pieces of my code can use it. Here is the helper function:
Function myGetActive(ByVal control As String) As ComboBox
If control = "width" Then
If width1_cb.Visible = True Then
Set myGetActive = Me.width1_cb
ElseIf width2_cb.Visible = True Then
Set myGetActive = Me.width2_cb
Else
Set myGetActive = Nothing
End If
End If
End Function
Unfortunately, this function is not functioning as expected. I have another line of code:
Set currentWidth = myGetActive("width")
currentWidth.Value = 5
which fails on the second line. I cannot figure out what I am doing incorrectly here - my best guess is that I am somehow not returning the actual combobox instance in my helper function but instead some sort of copy, but in my research I have not been able to figure out the correct way to accomplish this. Does any one know how I can make this function work as expected?
Update: Debugging efforts I have taken including stepping through the code line-by-line and putting a "watch" within the "myGetActive" function on both the width1_cb and the myGetActive variables. What I find is that the myGetActive variable gets set to type "ComboBox/Combobox" while the width1_cb is "Object/Combobox". What's more, the context for width1_cb is "Input_Window.UserForm_Initialize" while the context for the myGetActive is "Input_Window.myGetActive". The function does exit without any errors though.
Initially I just wanted to make a comment about 2 distinct ways for declaring variables:
Private currentWidth As ComboBox
or
Private currentWidth As MSForms.ComboBox
but I can't duplicate the issue you're having. Here is the code I tested with
Option Explicit
Private currentWidth As ComboBox
'Private currentWidth As MSForms.ComboBox
Private Sub UserForm_Click()
testCombos
End Sub
Private Sub UserForm_Initialize()
With Me.width1_cb
.AddItem "test1"
.AddItem "test2"
.AddItem "test3"
End With
With Me.width2_cb
.AddItem "test1"
.AddItem "test2"
.AddItem "test3"
End With
End Sub
Private Sub testCombos()
Set currentWidth = myGetActive("width")
currentWidth.Value = 5
Set currentWidth = myGetActive(vbNullString)
currentWidth.Value = 7
End Sub
Function myGetActive(ByVal control As String) As ComboBox
If control = "width" Then
If width1_cb.Visible = True Then
Set myGetActive = Me.width1_cb
ElseIf width2_cb.Visible = True Then
Set myGetActive = Me.width2_cb
Else
Set myGetActive = Nothing
End If
Else
If width2_cb.Visible = True Then
Set myGetActive = Me.width2_cb
ElseIf width1_cb.Visible = True Then
Set myGetActive = Me.width1_cb
Else
Set myGetActive = Nothing
End If
End If
End Function
.

Excel VBA - Code breaks when Excel opens

I just discover problem with my code, which not appears yesterday.
This is code from "Sheet1 (Calculator)":
Option Explicit
Private Sub Worksheet_Activate()
Sheets("Calculator").ComboBox1.ListFillRange = "Materials!B4:B7"
Sheets("Calculator").ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
Sheets("Calculator").Range("T18") = ComboBox1.ListIndex + 1
Select Case Sheets("Calculator").ComboBox1.ListIndex
Case 0
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G4:G5"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 1
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G6"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 2
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G7:G10"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 3
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G11:G12"
Sheets("Calculator").ComboBox2.ListIndex = 0
End Select
End Sub
Everything working fine, while Excel is opened. But if I save and close Excel and then reopen it, code breaks at first line under "Case 0" with error message:
Run-time error '438'
Object doesn't support this property or method
Then when I stop debuging and change item in ComboBox1, code works fine again and ComboBox2 is filled with correct data. Do you have any ideas where can be problem?
Here is the file.
Dont know if you initialized your ComboBox object properly.....but this should work:
Set ComboBox1 = Sheets("Calculator").Shapes(1)
ComboBox1.ControlFormat.ListFillRange = "Materials!B4:B7"
so mayb its because mainly you are missing the ControlFormat thing!?
Private Sub ComboBox1_Change()
with Sheets("Calculator")
.Range("T18") = ComboBox1.ListIndex + 1
if .ComboBox1.ListIndex > -1 Then .ComboBox2.List=sheets("Materials").range(G4:G5").offset(choose(.ComboBox1.ListIndex+1).value
end with
End Sub

How to Select All Text in TextBox After textBox.Setfocus Using Access VBA

I need to select all the text in a textbox of an Access form when I click (or double click) into it. i tried the following code, unsuccessfully:
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
thanks in advance.
You can use the code shown below. If it doesn't work, place a breakpoint at the first line of code. If it doesn't stop on your breakpoint, then your event is not recognized.
Option Compare Database
Option Explicit
Private Sub txt_CompraPreco_Click()
If Len(Me.txt_CompraPreco & "") = 0 Then Exit Sub
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
End Sub
I was looking for a solution regarding this problem, I have the same issue, however, I have a solution to it, I'm not sure if it's efficient, but here's my code:
'Declare a flag
Public flagDblClick As Boolean
'Double click event
Private Sub txtbox_DblClick (Cancel As Integer)
flagDblClick = True
End Sub
'Mouse up Event
Private Sub txtbox_MouseUp(Button As Integer, Shift As Integer, X as Single, Y as Single)
If flagDblClick Then
flagDblClick = False
txtBox.SelStart = 0
txtBox.SelLength = Len(txtBox.Value)
End If
End Sub
This code will resolve your problem (use with userform).
txt_CompraPreco.SetFocus
Me.txt_CompraPreco.SelStart = 0
Me.txt_CompraPreco.SelLength = Len(Me.txt_CompraPreco)
My trial and error found this.
If your textfield is formatted as a Standard Number and you have set the decimal places to a certain length, you will run into trouble when you enter a single digit. For example if your decimal places in the field properties is set to 2 and you enter "1", you will display "1.00". To get the entire field (1.00) selected, you must specify the .Text property when you determine the .SelLength (not the default .Value property)
Me.txtYourFieldname_GotFocus
Me.txtYourFieldName.SelStart = 0
Me.txtYourFieldName.SelLength = Len(Me.txtYourFieldName.Text)
End Sub
This works for me:
Dim bSelect As Boolean
Private Sub fieldX_Click()
If bSelect Then
'Select text only at first mouse click then user can click again
'and is able to put mouse pointer where he prefers
Me.fieldX.SelStart = 0
Me.fieldX.SelLength = Len(Me.fieldX)
bSelect = False
End If
End Sub
Private Sub fieldX_GotFocus()
bSelect = True
'Select text if field got focus via keyboard, Enter or TAB
'this is not enough if field got focus via mouse click
Me.fieldX.SelStart = 0
Me.fieldX.SelLength = Len(Me.fieldX)
End Sub

Cycling through some checkboxes on my excel page

I am VERY new to VBA (and know of it only within excel).
I am trying to cycle through some (but not all) checkboxes. They are currently named CheckBox1 through CheckBox15. Howe do I cycle through say for instance CheckBox5 through CheckBox10?
I guess I am hoping there is a 'method' similar to 'CheckType' for controls that will allow me to check name?
Here is what I have tried. Causes a Compile Error - Sub or Function not defined, and highlights Worksheet.
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 2 To 4
If Worksheets("ActiveX").Controls("Checkbox" & i).Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
While the above doesnt work, what is below does:
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 1 To 2
If Sheet2.CheckBox2.Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
Of course, the loop has no affect on the outcome, but it compiles and atLeastOneChecked turns from False to True when Checkbox2 is True. Note that Sheet2 has been named ActiveX. I clearly don't understand how Worksheet and Controls work. Could someone help?
After fixing the error described below, this still won't work. I simplified to the following:
Private Sub BoxCheck()
Dim ole As OLEObject
atLeastOneChecked = False
Set ole = Sheets("ActiveX").OLEObjects("Checkbox2")
If ole.Value = True Then
atLeastOneChecked = True
End If
End Sub
This doesn't work. It fails at:
If ole.Value = True Then
The error states: Object Doesn't support this property or method
This is true for OLEObjects, but not for Checkboxes. When I look at the properties of ole, I see that its Object property is set to Object/Checkbox and that this Object has a value. I guess that is what I should be referencing in my if statement, but I don't know how.
I think I solved the problem.
The value of the Checkbox is accessed by Referencing the Object property within the OLEObject I set...Like this:
If ole.Object.Value = True Then
Thanks for all your help. If someone has a more elegant solution, I would still like to see it.
Use CheckBox.Name
Example:
For Each cb In ActiveSheet.CheckBoxes
If cb.Name = "CheckBox5"
' Do stuff
End If
Next cb
To expand on #Parker's answer:
Private Sub BoxCheck()
atleastonechecked = False
Dim oles As OLEObject
For i = 2 To 4
'If you're using Shapes Controls:
If ThisWorkbook.Worksheets("ActiveX").Shapes("Check Box " & i).Value = True Then
atleastonechecked = True
End If
' If you're using ActiveX Controls
Set oles = ThisWorkbook.Worksheets("ActiveX").OLEObjects("CheckBox" & i)
If oles.Value = True Then
atleastonechecked = True
End If
Next i
End Sub
Sorry, just put together the previous answer without testing - that always fails. Just use the If loop based on what type of control you're using.