Enable/Disable Record Specific Controls in Access 2013 - ms-access

I'm trying to enable/disable some checkboxes based on another fields value for a specific record in a datasheet form.
If I use vba, it seems that the every record is disable/enabled (using the following code) no matter which event i place it under.
If Me.x = "N/A" Then
Me.y.Enabled = False
Else
Me.y.Enabled = True
End If
Furthermore, since its a checkbox control, i cant use conditional formatting like i do for textboxes and etc....
Any help would be greatly appreciated.
Thanks,

You can't. All controls in the detail section of a continuous form or datasheet form share one set of properties. If you disable a control, you disable all instances of it.
What you can do is place code in the OnDirty event of the checkbox:
Private Sub y_Dirty(Cancel As Integer)
If Me.x = "N/A" Then
Msgbox "Sorry pal!"
Cancel = True
End If
End Sub
It's not quite as intuitive, of course.

Related

Ms access - Disable tabbed (sub)form until main form's ID is not null

I have a main form (frmCustomer) bound to tblCustomers, and a tabbed subform with 2 pages, one bound to tblCustomerAddress and the other to tblContacts.
I don't want the user to be able to insert data in the tabbed form, until I have a valid CustomerID.
I tried (on current event of main form, before update)
If isNull(Me.CustomerID) then
Me.Page_Address.Enabled = False
end if
But it disables the tabbed form and keeps it disabled forever...
Thank you so much for any thoughts as I am clueless.
As well as the Current event, perhaps need to put code in another event, such as AfterUpdate of first control on main form that gets data input.
Then instead of If Then, simply
Me.Page_Address.Enabled = True
or
Me.Page_Address.Enabled = Not IsNull(Me.CustomerID)

MS Access - Change Field Value with Button

I have a button which, when clicked, I want to change the value of the Status field to Closed.
How can this be achieved?
I have tried Me.status = "Closed" and a few variations of it but suspect that I am not using the syntax.
UPDATE:
It is an unbound button (called "Close") within a Single form which has a bound table.
This is what I want to happen:
User clicks "Close" button;
The value within the "Status" field on the active record changes to "Closed"
How can this be achieved?
I have solved the problem.
This is the VBA code that I wrote which works perfectly for me. Hopefully others may also be able to use it.
Private Sub closeCase_Click()
Me.[Status] = "Closed"
Me.refresh
End Sub

ms access display inputs of textbox1 in textbox2 in realtime

i'm working on some VBA code for msaccess, basically i want to display the data i typed on textbox1 to textbox2. i have tried all the events available and it ain't work.
i have tried this
Private Sub input1_AfterUpdate()
output1.Value = input1.Value
End Sub
on key down and after update. it works on after update but i need to refresh the page before the inputs are loaded to textbox2.
what i need is realtime update, everytime i typed something on textbox1, it will display on textbox2, including the previous letters i typed.
thanks a lot guys, badly need a solution for this :(
Use Change event and Text property:
Private Sub textbox1_Change()
textbox2.Value = textbox1.Text
End Sub
We take .Text property of source textbox, because .Value is changed after the end of edition.

Event not Firing in MS Access VBA

I have a form in MS Access which has an image. The image has an Click event which opens a modal form. The modal form has an OK and Cancel button. When you click the OK button, an event is supposed to fire which tells the main form which button was clicked. (This is to simulate the DialogResult functionality in C#). However, the code in the event handler never runs.
The modal form has the following in the general declarations:
Public Event OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)
and the following code where the OK button is clicked:
RaiseEvent OnDialogBoxClose(NewHardwareBaseItemID, dlgresBtnOKClicked)
The main form has the following in the general declarations:
Dim WithEvents RespondQuickAddClose As Form_qckfrmHardwareBaseItemCreate
and the following event handler:
Private Sub RespondQuickAddClose_OnDialogBoxClose(NewRecordID As Long, DialogResult As DialogResults)
MsgBox "Responding to closing of the dialog box" 'Never happens
Me.Requery
End Sub
Can someone explain why the event handler is never called?
Thanks!
Background:
The purpose of all this is to allow a modal dialog box to add an entry, then return the ID of the entry back to the main form to set the value of controls. For instance, imagine you are filling out an insurance form, and you need to select a brand of car this is not there. You click on an icon which pops up with the modal dialog box to allow you to add the car brand. Then when you click OK, it takes you back to the insurance form and selects the brand of car you just created.
This follows an example I found here:
http://database.itags.org/ms-access-database/80292/
You're making your life way too complicated by applying concepts from a different development environment to Access VBA. While VBA does support WithEvents/RaiseEvent, there's no reason to get that complicated here.
The usual way to work with dialogs in Access is that instead of closing them, you hide them. This allows the code after the form was open to run while leaving the values in the form available for use in that code.
Sample code in the OnOpen event of a report that opens a form for collecting values to filter the report:
Private Sub Report_Open(Cancel As Integer)
DoCmd.OpenForm "dlgDateRange", , , , , acDialog, "ThisYear"
If IsLoaded("dlgDateRange") Then
With Forms!dlgDateRange
If .Tag = "Cancel" Then
Cancel = True
Else
Me.Filter = "[InvoiceDate] Between #" & !txtStart & "# AND #" & !txtEnd & "#"
Me.FilterOn = True
Me!lblDateRange.Caption = StrConv(Trim(("from " + varZLStoNull(Format(!txtStart, "mm/dd/yyyy"))) _
& (" to " + varZLStoNull(Format(!txtEnd, "mm/dd/yyyy")))), vbProperCase)
End If
End With
DoCmd.Close acForm, "dlgDateRange"
End If
End Sub
The dialog form has two command buttons, CONTINUE>> and CANCEL. The CANCEL button sets the form's tag to "Cancel" and sets the form's .Visible property to False. The CONTINUE>> button does nothing but set the form's .Visible property to False. Clicking either of those buttons allows the code to continue on the line after the form is open with the acDialog switch.
My philosophy is to make the dialogs as stupid as possible. The calling code has to know what it's looking for in the forms (i.e., you need to know the names of the controls you're reading data out of), but that could be gotten around by adding customer properties to the form. But then you have to know the property names, so you've just moved the ball.
I have also implemented this kind of thing by wrapping the dailog form in a class module, and then the calling context simply initializes an instance of the class and then pulls the values out of it at the appropriate time. But that's actually more complicated that the approach above.
well I don't agree to
"While VBA does support WithEvents/RaiseEvent, there's no reason to
get that complicated here."
I have worked on various VB6 and VBA project. Recently I coded VBA in excel where I raised an event from winform. Few things to be considered when doing so.
If you are calling non-modal winform in VBA with
withevents/raiseevent. It should work normally as expected. No major
workaround is needed
If you are calling modal winform in VBA. Withevents/raiseevents may
not function as per requirement. A quick workaround is to transfer data using public variables declared in the module file.
You will need to use the workaround and I believe it will work absolutely fine.

Hide a column programmatically in MS-Access

I want to hide or show a column based on variable data from a users selection. How do you set a column to hidden in MS-Access 2003?
For Example,
After user change event...
For Each ctl In Me.FormNameHere.Form.Controls
If (TypeName(ctl) = "Textbox") Then
If InStr(GetTextList(), ctl.Name) > 0 Then
ctl.hidden = True
Else
ctl.hidden = False
End If
End If
Next ctl
What is the best approach to this type of challenge?
Is there a more obvious solution?
Controls do not have a "hidden" property (no objects in Access have a hidden property). They do have a .Visible property.
For future reference, I suggest you familiarize yourself with the Object Browser in the VBE -- open the VBE and hit F2. You can then restrict your search to the individual libraries used in your project. It does take a while to get to the point where you understand the object model, though.
Also, you can rely on Intellisense to learn the properties/methods of an object, so in the code of the form you're working with, you can type "Me.MyTextBox." and the Intellisense dropdown will show you all the properties and methods of that particular control. It doesn't work for a generic control variable (as in your code) because different control types have different properties.
And, of course, the properties sheet gives the names of the properties, even though in code they don't always use the same orthography (usually they are the same with spaces removed).
Also, there are differences in how you might want to do this depending on whether it's a regular form or a datasheet form. In a datasheet, your controls also have .ColumnHidden and .ColumnWidth properties (setting those in any view other than datasheet view has no effect, and neither of those properties are available in the standard property sheet, but changes to them are retained when you save the form).
I answered a similar question to this not long ago to do with hiding columns on a datasheet. However you seem to want to hide textboxes arranged in a column on a form, is that correct?
Iterating over all the controls in the form could be slow if you have many controls. If you really need to use textboxes like that, then you could try group the 'columns' together then hide the groups. Another approach would be to use a listbox or datasheet to represent the data, where you can alter the layout of the columns directly.
I found the ColumnHidden property does the trick.
For Each ctl In Me.FormNameHere.Form.Controls
If (TypeName(ctl) = "Textbox") Then
If InStr(GetTextList(), ctl.Name) > 0 Then
ctl.Columnhidden = True
Else
ctl.Columnhidden = False
End If
End If
Next ctl
I got a hint from this related question.
A one-liner approach is using:
forms(fname).Controls(ctrlname).columnhidden = false
where
fname is name of your form
ctrlname is name of your control