How do I get the updated value of a textbox in VBA Access in a custom Sub? - vba

This is for VBA Access 2003
I've got a textbox I want to use as a filter for a list box rowsource command. I also have a checkbox which adds another filter for the same rowsource command. I've only programmed in C# and I'm trying to write a single Sub which will simply set the RowSource regardless of if my textbox filter is changed or if my checkbox filter is changed. However, my textbox is giving me problems.
If my checkbox filter changes and I run my method the textbox.Text throws an error saying that it must have focus - Text is null. If I do a null check on that property it throws an error saying the control must have focus.
I've used the .Value property, but for whatever reason it doesn't update to the newer values.
My current attempt:
If Me.txtClientFilter.Text = Null Then ' Error 2185
filter = Me.txtClientFilter.Value
Else
filter = Me.txtClientFilter.Text
End If
Should I
Manually add focus then remove it everytime I want to check a
control?
Duplicate my code in each control's event Sub?
Manually set
the .Value property when the change happens?

I fixed my problem with some code which I'll show below. I don't know what was happening behind the scenes, but the .Value was not getting updated with the .Text value. I decided to set it explicitly, which then caused the entire text box value to be selected.
I ended up with the following code which explicitly sets the .Value of the control and also reset the cursor to the end of the text in the control. Thanks to some guy named Brent Spalding here for the cursor code.
Private Sub txtClientFilter_Change()
Me.txtClientFilter.Value = Me.txtClientFilter.Text
ProcessFilter
txtClientFilter.SelStart = Len(Me.txtClientFilter.Text)
txtClientFilter.SelLength = 0
End Sub

Related

Conditional visibility on MS Access Form - how to write in VBA or Macro

I have some very (very) basic MS Access knowledge. I'm trying to expand a bit into either VBA or macros as I'd like to put in some conditional visibility for my form. Basically, I have a checkbox. If it's checked, I want three or four more fields to pop up. Someone was able to point me to a basic VBA formula of if (this checkbox) = true then, (fieldx).visible = true, else, (fieldx).visibility = false, end if.
But I'm so new to this that I need more help and explanation. I tried putting it in but couldn't get it to work (no error message, just nothing changed at all).
Specific questions:
-Does this formula seem right?
-If I want multiple fields to be visible, can I combine them into one formula or should I create a new "if" statement for all?
-Where do I enter this code? I'm running the Office 365 version. For all I know, I'm not even putting it in the right place.
-How do I determine the field names to replace the (this checkbox) and (fieldx) in the formula? I tried entering the name I title the fields as, but with the spaces in the name I got an error message, and without the spaces nothing happened. Is there a specific naming convention to turn the field names into formula-appropriate titles? Is the name listed somewhere?
-Once I get the formula entered, is there something I have to do to get it to run/take effect? I tried saving, closing and reopening with no changes.
-Is this the best way to go about this?
If there's anything else you think I should know, I would love to hear it - but please keep in mind I'm very new to this so if you could keep it at "dummy" or ELI5 levels of explanation, I'd appreciate it!
after creating a form with 4 textboxes and a checkbox put the form in design mode (lower right corner has design mode selected, select a textbox and hit property sheet on the ribbon (or f4).
On the property sheet note the visible property. set the visible property to false. Now the textbox will be invisible when the form starts.
Tip you can select all the textboxes at the same time and set their properties all at once.
Every control on the form and even the various parts of the form have properties you can set and play with. For instance you can give any name you want to any control. On the property sheet go to the other tab and set the name property.
Tip: choose a name you you will remember without having to look it up and describes the controls function.
Next select the checkbox (not the checkbox's label). On the property sheet go to the event tab and select the on click event. hit the ellipsis and choose code builder. Access is Event Driven. We want the textboxes to appear when the checkbox is selected so we put that code in the checkbox click event.
after choosing code builder we get the code window where we can browse among all the events for all our forms. for now all you should see is something like:
Private Sub mycheckbox_Click()
End Sub
So insert some code to handle the checkboxes like:
Private Sub mycheckbox_Click()
If mycheckbox = True Then
txtbox1.Visible = True
txtbox2.Visible = True
txtbox3.Visible = True
txtbox4.Visible = True
Else
txtbox1.Visible = False
txtbox2.Visible = False
txtbox3.Visible = False
txtbox4.Visible = False
End If
End Sub
now when the checkbox is not checked no textboxes are visible.
but when the checkbox is checked they appear

Access VBA - Type Mismatch when working with Combo Boxes (Multi Valued Fields)

I have a form that displays a single record at a time, and allows the record to be edited by displaying it all in Text Boxes and Combo Boxes. Some of these are ComboBoxes based off of lookup fields, where the values are pulled from a preset list (multi-valued fields).
After that, I have a class module with a property defined for each field in the record (a FirstName Property, a LastName Property, an Address Property... you get the idea). I have a function that creates an object from the class, and then takes the values from the form and assigns them to the corresponding property. This works fine for most of the fields, but as soon as it gets to the first Combo Box (multiple selection), it throws a Type Mismatch error. Code I'm using is:
If Me.Issue <> vbNullString Then
ProfileObj.Issue = Me.Issue
End If
'Me.Issue is the combobox on the form - this is in the forms module
'ProfileObj is the class instance
In case you wanted to see the Property in the class module for the ProfileObj object:
Private ProfileIssue As String
'... other variable declarations
Property Get Issue() As String
Issue = ProfileIssue
End Property
Property Let Issue(rData As String)
ProfileIssue = rData
End Property
I've also tried using Me.Issue.Value, Me.Issue.Text, and Me.Issue.Column(0) to refer to the Combo Box, but none of these have worked either. I even tried using CStr(Me.Issue), to no avail. How can I take whatever's displayed int the combo box and assign it to a String variable?
I figured it out...
I needed to read the text from each Combo Box with each box's .Text property. I had tried that inside of the If statements, but not for the actual comparison that the If statement was built on. The working version of the code now reads:
Me.Issue.SetFocus 'You have to set focus in order to read the text, dont ask me why
If Me.Issue.Text <> vbNullString Then 'This is where my code wasn't working
.Issue = Me.Issue.Text 'I had tried it here before, but the code never got there since the line before failed
End If

How to see if a datagridviewcheckbox is checked or not

I have created a datagridview with a DataGridViewCheckBoxColumn. Initially I read in my data and set the value of the checkbox to true if the data is there and false if it is not. This works fine, but If I change the state of the checkbox to either checked or unchecked from what it was previously, I can't read this change. When I try to save the data it always shows, the checkbox's original state.
How can I re-read the checkbox's value after it has been clicked?
I tried the solution show here:
How to check a checkbox created with VB's DataGridViewCheckBoxColumn on Runtime
using: DirectCast(DataGridView1(0, 2).Value, Boolean)
but it does not work for me. I always get the original state of the checkbox when I look at it. If the box was originally checked and I uncheck it, this value will still show True.
You may be looking at the wrong row (you are looking at the first row only with the index of 0). You can use the DataGridView_CellEndEdit Event to find the value of the checkbox, like so...
If e.ColumnIndex = 2 Then 'this is the columnIndex of your checkbox column
If MyBindingSource.Current("CheckBoxColumnName") = true then
'the column is checked, so do something
End If
End If

Access Subform Source object

What I am trying to achieve is for a combo box (Combo_sf) selection to dictate the form in the subform control (sf_record) I have about 10 forms, their names are in the combo box data. I am new to VBA and am not sure if my approach is right:
Private Sub Combo_sf_AfterUpdate()
Dim strLoadTable As String
strLoadTable = "Form." & Me.Combo_sf.Value
MsgBox strLoadTable
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
End Sub
I have placed this in the combobox's after update event but when I make my selection nothing happens in the form. Am I approaching this right or would another way work better?
Your approach should work. I put a combo box named cbxSubform on my main form and added one line of code to its AfterUpdate() event handler...
Private Sub cbxSubform_AfterUpdate()
Me.mySubform.SourceObject = Me.cbxSubform.Value
End Sub
...and changing the selection in the combo box switches the subforms immediately. Are you sure that the AfterUpdate() code for your combo box is actually firing? (You could add a MsgBox or a Debug.Print to check.)
It could be this line which is tripping you up:
strLoadTable = "Form." & Me.Combo_sf.Value
What is your form object called? If your form is called Form.myTableName it could be the . that is throwing it out, try setting it to a form without a dot in its name.
In this line, it seems the code attempts to change the SourceObject property of a Form object.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.Form.SourceObject = strLoadTable
However, SourceObject is a property of a subform control, not the form contained in that control. So if the subform control is named sf_record, do it this way.
Forms![frm_Mnu_Manage Configuration Settings]!sf_record.SourceObject = strLoadTable
Also, if the after update procedure runs from [frm_Mnu_Manage Configuration Settings], you can use Me to refer to the form.
Me!sf_record.SourceObject = strLoadTable
Finally, if Me.Combo_sf.Value is the name of a form, you don't need to prefix its name with "Form.". It worked either way in my test, but I would just leave off "Form.".
strLoadTable = Me.Combo_sf.Value

MS Access Force Update on Combobox

How do I force an update when the ComboBox value is changed in code. Below is piece of code I have tried but does not seem to work
If (Not Mid(sCode, 1, 2) = ddlLevelID1) Then
ddlLevelID1 = Mid(sCode, 1, 2) 'force change/force AFTER_UPDATE event to run.
End If
Assuming ddlLevelID1 is the ComboBox:
ddlLevelID1.value = foo
will change the value. I do not believe you can link a value displayed in a ComboBox to a variable value without pushing changes up to the userform after the value is changed.
Regarding the AfterUpdate method, from msdn:
Changing data in a control by using Visual Basic or a macro containing
the SetValue action doesn't trigger these events for the control.
However, if you then move to another record or save the record, the
form's AfterUpdate event does occur.
http://msdn.microsoft.com/en-us/library/office/bb238392(v=office.12).aspx