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

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

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

How to use UserForm CheckBox to change Control Content CheckBox in Word Document?

I created an UserForm with a checkbox 'cbxYes' and a Content Control checkbox 'docCbx' in the Word document. I want to be to check off the checkbox 'cbxYes' in UserForm that then changes the Content Control checkbox in the Word document.
So the input is from the UserForm checkbox and the output is the Content Control checkbox.
I have tried multiple searches on how to do this, but I could not find exactly what I needed. Most of the searches were related to Excel. And honestly, I don't know what I'm doing. Please. The correct help is greatly appreciated.
Private Sub cbxYes_Click()
Dim oCC As ContentControl
If cbxYes.value = True Then
cbxYes.value = "True"
ActiveDocument.docCbx_Yes.value = True
Else
cbxYes.value = "False"
ActiveDocument.docCbx_Yes.value = False
End If
End Sub
The error I got was:
run-time error '438': Object doesn't support this property or method.
The code shown in the question would be for ActiveX checkboxes, rather than content controls. (Just to make things really complicated, Word also has checkbox form fields that need yet another code syntax.)
There's no way to refer directly to a content control name via the Document object - it must be done over the ContentControls collection. Content controls can be assigned a Title and/or a Tag in the Properties dialog box.
More than one content control can have the same name or title, which makes the code a bit complicated. Querying Document.ContentControls("Title") returns a collection (all the content controls with that title).
If you know which one, then it's possible to pick it up directly (rather than working through a collection) using the Item method, specifying that it's the n content control (the index in the order of content controls). This is usually used when one knows there's only the one.
Note, please, also, that in order to "check" or "uncheck" a content control checkbox the Checked property should be use. So:
Private Sub cbxYes_Click()
Dim oCC As ContentControl
Set oCC = ActiveDocument.SelectContentControlsByTitle("docCbx").Item(1)
If cbxYes.value = True Then
'cbxYes.value = "True" not needed
occ.Checked = True
Else
'cbxYes.value = "False" not needed
oCC.Checked = False
End If
End Sub
Assuming that "docCbx" is the title of the content control you can replace
ActiveDocument.docCbx_Yes.value = True
by
For Each oCC In ActiveDocument.SelectContentControlsByTitle("docCbx")
If (oCC.Type = wdContentControlCheckBox) Then
oCC.Checked = False
End If
Next
and the equivalent, but using False, for the other branch of your code. The above code will update all checkbox type content controls with that name (the names do not have to be unique within a document), except controls that have been marked as "Contents cannot be edited" - they will remain unchanged. It deals with the situation where there aren't any content controls with that name by doing nothing.
If "docCbx" is the value of the Tag, you would need the following instead:
For Each oCC In ActiveDocument.SelectContentControlsByTag("docCbx")
If (oCC.Type = wdContentControlCheckBox) Then
oCC.Checked = False
End If
Next
If docCbx is something else, I would suggest that instead you give Titles and/or Tags to your content controls and use the above approach. Otherwise, you should modify your question to state precisely how you are naming the control "docCbx".
You get the error because adding content controls to a document does not create new members of the document object, whereas adding a form control to a user form does create new members of the Form object. (Documents do in fact have a mechanism that works more like the Form object but it has been deprecated for a long time.)

How to reference tables/queries/reports in access using an alias name?

In my Access database, I have tables with the naming convention of "t_customers" or "t_orders". I have a combo box which lets me pick from these options. For a better look, I want to have the options in the combo box without the "t_", so one of the options is just "customers". Then in VBA, I can access the combo box's value of "customers" and then pass that as a parameter to my function which will export the contents of the associated table "t_customers" to an excel file.
Is this possible to do? I can think of using a select case statement and for each combo box value I manually assign the related table value, but there has to be a better way. Is there some sort of short cut that does this in Access using a relationship or similar feature without coding? Or is there a simpler way to code this?
EDIT:
I am also trying to get the value of the current item selected in my combo box named "cbTable". I have no macros attached to the combo box. I have a button that when pressed it runs a macro which calls my VBA function. In the function I have this code:
Dim cbValue As String
cbValue = cbTable.Value
MsgBox (cbValue)
I get this error:
Runtime Error '424'
Object Required
The error is on the
cbValue = cbTable.Value
line. What is wrong with this code and how do I get the current value of the combo box?
Thanks in advance.
You can set your RowSource for your combobox to
SELECT MSysObjects.Name, Replace(MSysObjects.Name,"t_","") AS Expr1
FROM MSysObjects
WHERE (((MSysObjects.Type)=1) AND ((MSysObjects.Flags)=0));
Then set the Column Count to 2, the Column Widths to 0;2 (the second number just needs to be any positive number) an the Bound Column to 1.
Now when you go to call your function the value of the combobox will be the real name of the table, t-customers, but what you saw on your form was customers.
As for accessing that combobox a few things need to be in place.
Let's say you have a form, Form1, and your combobox, cbTable, is on that form.
So when your form runs it looks like this
Now let's say you have an OnChange even for that combobox
Notice that when you type cbTable it appears in the intellisense (ctrl+space). This tells you that your object is accessible at this scope.
Now if you were in another module where that variable is out of scope you would get this error when you try to compile.
Because Module1 has no idea what cbTable is. However, you can reference anything so long as it's fully qualified. So if we run this test with your form open, and "orders" selected all 3 of these lines are equivalent
Forms("Form1").cbTable
Form_Form1!cbTable
Forms("Form1").Controls("cbTable")
Notice at the top of all my modules it says Option Explicit. This forces your code to basically be checked for syntax/scope validity before you run it. Tools > Options

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

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

How to get value of a Yes/No checkbox control when passed to another class?

I have a form TForm based on table T. There is a Yes/No field Choice in T. To add this field to TForm, I added a checkbox ChoiceCheck to the form. I am trying to get ChoiceCheck's value in another class, SQLPreparer, which I am using to construct SQL statements based on values in TForm:
Private Function controlToValueStr(ctrl As Control) As String
If ctrl.name = "ChoiceCheck" Then
controlToValueStr = ctrl.Value
Else
'return the control's value wrapped in quote marks
controlToValueStr = quoteWrap(ctrl.Value)
End If
End Function
However, when I call SQLPreparer.controlToValueStr() and pass ChoiceCheck, the ctrl.Value statement gives me this error:
Run-time error '438':
Object doesn't support this property or method
I'm able to get the value in the class for TForm itself as follows:
Me.ChoiceCheck.Value
All of the text box controls of type string work in both situations. I'm also having this problem with a textbox whose type in T is a Long.
The problem was that the I had named the label ChoiceCheck, not the checkbox itself. The same issue was present with the textbox. In both cases, changing the control names in Design View to match the VBA code resolved the issue.