Access VBA: change column width inside subform - ms-access

In my Split Form Form_name, I have the subform Subform_name.
Subform_name has different columns, such as Column_1.
I would like to change the width of Column_1 with the use of VBA.
How to do that?
I have tried with those options but they don't work.
Forms![Subform_name].[Column_1].ColumnWidth = 0
Me.[Subform_name].[Column_1].ColumnWidth = 0
I don't know if this is an useful information, but the form Form_name is a Split Form.

Try to use the Method DoCmd on the Object "Subform_name".
"Forms![Subform_name].[Column_1].ColumnWidth = 0 " is a Property .
You must distinguish between :
Properties and Events .
Methods Versus Functions.
The 4 Previous are applicable to VBA Objects .
You can find all VBA Objets :
Alt+F11 to open you VBA Editor.
F2 to run Object Explorer
On the Left Side Column, y'll find all Classes of all Objets .
Choose your Class , then Object .
You can make a command button with Subform_name_load and you write :
Column_1.ColumnWidth = 0.
The property has to be mixed with en event .
You can Try with the Method DoCmd which is useful for Forms and Also The Recordset Method.
Regards.

Related

Microsoft Access SearchForRecord macro action useless?

I have been trying to incorporate a built in macro action (SearchForRecord) in MS Access, however cannot get it to work for the life of me. There is minimal resources available online for this operation, and I've noticed that other people have struggled with the same issue.
I made a test database just to see if I could get it to work in the most basic form. I made a table with 3 columns (ID, Name, Colour) - I turned the table into a tabular form using the Form Wizard. I created a text box with a search button.
I then made a macro operation:
SearchForRecord
Object Type: Form
Object Name (Name of the Form) "frmNewSearch"
Record: First
Where Condition: ="txtIDSearch = '" & [Forms]![frmNewSearch]![txtSearchBox] & "'"
I took the where condition syntax directly from https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/searchforrecord-macro-action
I set the button click event to the Macro that I made.
In theory, I enter the ID into the txtSearchBox and it should bring up the appropriate record within the same frmNewSearch form.
When I try this, nothing happens and it just sits on the first record. I am using MS Access 2016 - is the macro action maybe just not supported in this version?
If there is another way at approaching this it would be much appreciated!
Cheers
Referenced article states:
Note the equal sign (=) at the beginning of the expression, and the
use of single quotation marks (') on either side of the text box
reference:
="Description = '" & Forms![frmCategories]![txtDescription] & "'"
Have to actually type = sign into argument. (Yes, I hear your ranting and cursing, but that's life.)
I presume txtIDSearch is name of textbox. The criteria must use name of field, which you say is ID. If ID is number type, don't use apostrophe delimiters (apostrophe delimiters are used for text fields, # delimiter for date/time, nothing for number type). So result will show like:
Where Condition: = ="ID = " & [Forms]![frmNewSearch]![txtSearchBox]
or since code and controls are on same form, simply:
Where Condition: = ="ID = " & [txtSearchBox]
However, both fail if form is a subform. This is because form is not open independently in Forms collection. A reference incorporating parent form name fails as well. Use VBA code methods.

Obtain Value from MS Access Checkbox

I have an Access database in which I store some data, and that database has 13 tables plus a reference one.
I want to make a form where there are several checkboxes and a button. Each chekbox represents a table, and every table selected will be joined inside a query writen in VBA, associated with the button click.
I've already made the same thing in Excel, and it works perfectly, so the only problem here is that I don't know how to access the checkbox value and use an IF condition to get the correct SQL string.
To make it clear, here I have a IF statement for one of the checkboxes in Excel:
If Range("B8").Value = True Then
CTODStrc = ", CTODTYPE, CTOD.TEMPERATURE, VALIDITY, DELTAR, DELTAL"
CTODStr = " JOIN CTOD ON REF.ID = CTOD.REF_ID"
JoinStr = JoinStr & CTODStr
Columns = Columns & CTODStrc
End If
SQLStr = RefStr & JoinStr 'Query sentence
The SQLStr is the query text, and it has a prior "select" string which is added.
In Excel, the cell B8 was associated with the checkbox, but in Access I have to make this condition using a checkbox thats in the form - how can I do it?
I've tried Me.CbName.Value, but it says the command is not supported.
Thank you.
The checked state of a checkbox is given by the Value property of the checkbox control. This property may be 0 (unchecked), -1 (checked), or Null for a block-filled triple state checkbox.
Since the Value property is the default property for a checkbox, and assuming you are not using a triple state checkbox, you should be able to use simply:
If CBName Then
' Do stuff
End If

Dynamic Query in a MS-Access form

I have a form in Microsoft Access (2016) where I have a dropdown list named KID. When the dropdown list looses Focus I want to run a query that would look like this:
SELECT max(Cnt)
FROM Table
WHERE KID = ValueOfDropdownList
The result of this query should be usable in a variable in the VB Code of the Event.
Unfortuantly I am restricted to Access at the Moment. Am I even using Access the right way (Choose from dropdown in form -> Event -> SQL Query in VB -> Display in form) or are there better ways to do that?
Thanks for your help in advance!
Firstly I would use the AfterUpdate event of the combo box, otherwise it will fire even if someone just tabs through the control.
Secondly if this is just to set a variable you would probably be better to use a Domain function in this case a DMax()
iVariable = DMax("Cnt","Table","[KID] = " & Me.YourComboControlName )
This assumes KID is a number field. If KID is text you would need to delimit the criteria like this;
iVariable = DMax("Cnt","Table","[KID] = '" & Me.YourComboControlName & "'" )
Lastly if you want this to update as you scroll through records you would need to add the same code to the OnCurrent event of the form. That fires when the record changes.
If it is for display purposes you can't use the same technique on a continuous form if this is setting an Unbound control, you would need to pull this in to the underlying forms recordset.
You can use DMax:
YourVariable = DMax("[Cnt]", "[Table]", "[KID] = " & Me!DropdownListName.Value & "")
In case KID is text, apply quotes:
YourVariable = DMax("[Cnt]", "[Table]", "[KID] = '" & Me!DropdownListName.Value & "'")
FYI there is another option. As the data source of your combo,
use a query returning 2 columns, like select KID, max(cnt) from table1 group by KID.
Then in your text box, retrieve your value using =combo1.columns(1).
For this to work you need to specify in the combo properties that it has 2 columns. In the formula, don't forget that those combo columns are 0 based.
This is the most efficient solution when you need to retrieve several values linked to the combo (and you don't have millions of records).

Cannot Get DLookup Function to work in Access 2013

I am trying to get the Dlookup function to work in Access 2013, but i just cannot get it to work, heres what i have so far :-
I have a query called qry_VehicleOverview in this query there are Two fields called VehicleNumber and DateLastExam
I have a form, there are a number of fields, two of them called Vehicle1 and DateLastExamV1, in DateLastExam1 ! am trying to reference the relevant exam based on Vehicle1 from qry_VehicleOverview field, so when a Vehicle Number is added to Vehicle1 it displays the correct Exam in DateLastExam1
first of all I create a combo box in the form, called Vehicle1 and referenced it to VehicleNumber from qry_VehicleOverview
then i created a text box in the form, and called it LastExamVehicle1, in the control source of this field I added the DLookup function :-
=DLookup("[DateLastExam]","qry_VehicleOverview","[VehicleNumber]=""" & [Vehicle1].[Text] & """")
Then chose After update in Event tab and Selected code Builder in here I added :-
Private Sub LastExamVehicle1_AfterUpdate()
Me.LastExamVehicle1.Requery
End Sub
but when run the form, first of all i get an error of #Type in the field, when i change the value in Vehicle1 the eror then changes to #Error
I create a combo box in the form, called Vehicle1 and referenced it to
VehicleNumber from qry_VehicleOverview
If that is so, there is no need for DLookup as you already have the value.
Set the RowSource of Vehicle1 to qry_VehicleOverview and the count of fields for the combobox to 2 and use this ControlSource for your textbox:
=[Vehicle1].[Column](1)
It will automatically update.
Don't use [Vehicle1].[Text], use [Vehicle1].[Value] instead. Or just [Vehicle1] .
(.Value is the default property)
.Text is only valid while the focus is in the control, and in AfterUpdate it isn't anymore.
.Text is mainly useful in the Change event, but that wouldn't make sense for your case.
Edit
You probably need the "full path" to the control in DLookup. For better readability I suggest using single quotes.
=DLookup("[DateLastExam]","qry_VehicleOverview","[VehicleNumber]='" & Forms!yourForm![Vehicle1] & "'")
Also please double-check all names in your form (and then in your question). E.g. from your description, your event procedure should read:
Private Sub Vehicle1_AfterUpdate()
Me.DateLastExamV1.Requery
End Sub

Set Form Properties on SubSubform from the Main Form with VBA

In Access 2007 (or 2010), I need to set the properties on a subform, embedded within the first subform, from the main form using VBA. To make it clear which form I'm talking about, I'll call the this form "subSubform" as I do in my code (well, subSubform is actually the name of the sub-subform control).
When the form loads I have a subform control on my first subform that's blank (does not contain a form source object). If needed, I fill out the SourceObject property of this control with the name of a valid form, as well as the LinkMasterFields and LinkChildFields properties. This all appears to work fine because I do get an expandable plus sign that otherwise wouldn't be there.
Next I try to set the RecordSource for the subSubform and I get an error:
2455 You entered an expression that has an invalid reference to the property Form/Report.
Here's the code I'm using:
Me!subform1.Form!subSubform.Form.RecordSource = sSubformSQL
'and I've tried this with the same bad results
Me.subform1.Form.subSubform.Form.RecordSource = sSubformSQL
'and this too
Forms("frmQuery").subform1.Form.subSubform!Form.RecordSource = sSubformSQL
'And I've tried this. It fails too, on the last line with the same error:
Dim frm As Form
Set frm = Forms("frmQuery").subform1.Form
Dim frm2 As Form
Set frm2 = frm.subSubform.Form
I've tried setting a timer and waiting one second to run the above code, just in case it takes some time for the form to load. But this makes no difference.
I've tried running code from the first subform instead of from the main form, but that doesn't help either. Still fails with the same error.
FWIW, both my subform and my subSubform are DataSheet views. I don't think that makes any different on what I'm trying to do here (although I'm well aware of the performance problems involved so don't scream at me).
I recognize that it's probably a very odd request. But I'm creating a dynamic query interface that needs this.
Any ideas how to set form properties on a subSubform?
The problem is not that you setting properites of a sub form. The format of:
me.subform1.form.subform2.form.RecordSource = sql
s+Should work just fine.
The problem is you are trying to nest a continues form (or datasheet) in side of a continues for (or a datasheet). This is NOT permitted.
The workaround is to simply place the two sub forms side by side and don’t' nest.
You can still get the second (child child) form to follow by using the link master settings.
In the link child/master settings for child 1, you place:
linkChildFields main_id (whatever is the name of the field in
this sub-form that is used to relate back to the parent table)
LinkMasterFields [ID]
In the link child/master settings for child 2 form you place
linkChildFields main_id (whatever is the name of the field in
this sub-form that is used to relate back to the parent table)
LinkMasterFields [child1].[form].[ID] ("masterForm" is the name of
the control you used to hold the master form.
So you can model a many to many, but you cannot nest, but as noted the above gives you the same result anyway. So a form will look like this one:
You also likly in the on-current event of the left side (child 1), will want to place a requery of the right side when you navagate - as it often will not update automatic for you.
So:
me.parent.Child2.Requery
Generally the Form property of a subform control gets a "valid reference" only after the subform is shown. Thus, if you want to execute a subSubform.Form access as you do in
Me.subform1.Form.subSubform.Form.RecordSource = ...
you have to make sure that subSubform is visible before the access is executed. The easiest way to do it is to let the subSubform set it's source on his own while it loads (in it's Form_Load sub).
For further details and explanations see my answer in Microsoft Access runtime error 2455 when trying to access grandchildren forms from child form where the problem is the same.