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

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.

Related

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 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

MS Access: How to bound text box of form with query?

I created a form in MS Access 2010 and added a textbox here. Then I created a simple query (for example SELECT 10 AS studval;) and tried to set in Properties (of textbox) -> Data -> Control Source this query, but I got error #Name?.
How do I fix this error?
All names of query, textbox, query return values are correct. Or maybe are there any other ways to bound textbox and custom SQL query?
There is no easy way to do it, but it is possible using the form's On Activate event. First set up a query (Query1) with a single value called "studval" then open the form properties and add an Event Procedure for On Activate. It should look like this:
Private Sub Form_Activate()
Dim myString As String
myString = CurrentDb.QueryDefs("Query1").OpenRecordset.Fields("studval")
Me.Text0.SetFocus
[Text0].Text = myString
End Sub
You need to set the Control Source of the form to the query rather than the control source of the text box. A text box control source can only refer back to it's form's control source.
If you want just one text box bound to a query you have to create a subform linked to the parent form with that text box in it.

Casting problem after obfuscation with Dotfuscator

I'm trying to obfuscate some VB.NET 2003 app.
The resulting assemblyes are obfuscated and "run" with some errors.
I cleaned all potential reflection problems, but I'm not being able to read the selected value of a combobox.
I load the Combobox using their Datasource properties, using a collection of "VTPair" (a class created by me with 2 properties: one of string type and other of object type to store the value)
This combobox handle pairs like "Male | M" or "Female | F".
When trying to see what is selected, I use if mycombo1.SelectedValue = "M" then
This code, after obfuscation, throws me an exception that cannot cast type "XX" to string "M".
So, I changed the code to something more correct, explicitly casting the selected value to String:
if ctype(mycombo1.SelectedValue,string) = "M" then
But the error is the same.
Debugin my original code, the SelectedValue property is of type "Object" but it is a string.
I tried using the ComboBox.SelectedItem property that is also an object but this time what is inside is of type "VTPair" (my own class) and then trying to access its "Value" property (which is of type Object) and trying to cast to string fails again.
Does anyone have an idea to "translate" this piece of code to work OK after Dotfucate it?
Thanks!
From MSDN:
ListControl.SelectedValue Property:
Gets or sets the value of the member property specified by the ValueMember property.
So whatever property NAME you set for the ValueMember property will be used when you use the SelectedValue property. So you may need to exclude from obfuscation, the property which you specify via the ComboBox.ValueMember property.
Not sure of the VB Syntax but in C# I think you would want something where the right-hand side is typeof(MyType). This way the type will be obfuscated to match the renamed type.

Show yes/no in bool column of gridview bound to datasource in Winforms?

I have a gridview that is bound to a datasource (Windows Forms, VB.NET). One of columns is a property of type boolean, and I want to show "yes/no" in the column instead of 0/1 or "true/false". Is this possible? Can you edit displays of columns that are bound?
I have encountered the same problem, unfortunately I didn't find an elegant solution.
Three workarounds are proposed:
Add another property to the data source's class, which returns your string representation of the boolean property. Hide the column showing the boolean value, display the column showing the string value.
Add an unbound string column, populate that column with the appropriate value for each row, and hide the boolean bound column.
Create a wrapper class for your data class, which exposes the properties as you'd like them to be shown in the datagrid.