ms access display inputs of textbox1 in textbox2 in realtime - ms-access

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.

Related

Enable/Disable Record Specific Controls in Access 2013

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.

Openning a new form from a tab

I'm creating forms with VBA/Access to access my database.
In a form, I have a *lst_sinistres* listbox that displays the results of my SQL query and when I doubleclick on one of the results it opens me another form with thanks to this code
Private Sub lst_sinistres_DblClick(Cancel As Integer)
DoCmd.OpenForm "F_SINISTRE_MRH", acNormal, , , , , Me.lst_sinistres.Value
End Sub
I wanted to change my form, and add tabs to make it more ergonomic. So I placed my *lst_sinistres* listbox inside a tab.
The problem is that when I doubleclick on one of the results in this listbox (now placed in the tab), the form *F_SINISTRE_MRH* does not open.
Does someone have an idea of ​​where the problem might come?
Thank you
A quirk of VBA control events is that event code can become detached from the control object. Things that cause this tend to be re-naming controls and copy/pasting similar code between controls. To move your listbox onto a tab control you needed to cut and paste it temporarily. That broke the link between the written code and the object name. When the code and object are properly linked, [Event Procedure] shows up in the property sheet (as suggested by #4dmonster).
If you are in the VBA editor, choosing Debug->Compile will search through all the code and re-link event code with like-named controls. This step is worth a try before re-writing because you may end up with orphan blocks of
Private Sub OldControlName_DblClick(Cancel As Integer)
MsgBox "Why don't I work anymore?"
End Sub
that are treated as Form-level subroutines that just happen to never be called.
pT

Can't change Text of TextBox in Report_Open

All I want to do is set a textbox or label's text to something dynamic when the report is opened with a click of a button in another form. I've solved everything except actually changing the text.
This code gives run-time error 2478 on SetFocus:
Me.tFilial.SetFocus
Me.tFilial.Text = filialen
Without SetFocus I get a run-time error saying the text can't be changed without switching control to the control in question.
What is allowed where is always in question in Access, it seems. How do I solve this? Can I set the value on the buttonclick in the other form with
Reports![rptPressSchema]![tFilial].text="Hello"?
I would be happy to use a label instead, if that solves it. But the bottom line is I can try to do this every which way but I thought I'd ask you for advice as to best practice, as this must be a very common task indeed.
From the Access help:
While the control has the focus, the Text property contains the text data currently in the control; the Value property contains the last saved data for the control. When you move the focus to another control, the control's data is updated, and the Value property is set to this new value. The Text property setting is then unavailable until the control gets the focus again. If you use the Save Record command on the Records menu to save the data in the control without moving the focus, the Text property and Value property settings will be the same.
Basically, the .Text property serves no purpose in a Report because individual controls cannot receive the focus. However, as #Remou stated in his comment, you can simply replace .Text with .Value and your code should work fine (no need to set focus when updating the value).
spent a lot of time for searching and trying. Finally figure the things out...
To dynamically set the TextBox content, it is convenient to use
tbTest.Value = "hello"
but the trick is if you are using this On Open, it will be in trouble...
Run-time error '2448'
You can't assign a value to this object.
So you need set the value On Load
Private Sub Report_Load()
Me.tbTest.Value = "hello"
End Sub
Private Sub Report_Open(Cancel As Integer)
'Me.tbTest.Value = "hello"
End Sub
I see nowhere for any explanation of this, my guess is for Open event, the object is still not initiated (document for explaining Load and Open event)...

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.

How to refresh listbox

What's the best way to refresh a list box in MS Access? The way I have tried it doesn't seem to work :-/
This is my current method (onClick event of a button on the same form):
Me.orders.Requery
I have also tried
orders.Requery
and
Forms!currentOrders.orders.Requery
but none of them seem to refresh the list box's contents - I see no new items, even though I know that there are some. Closing off the form, and then re-opening it does show the new items, however.
You could try and requery the form first and then requery the listbox
You need to use a temporary textbox, then your listbox will refresh automatically.
The following solution worked for me. (I don't know why, but using direct value from searchbox didn't work for me.) i have used no button. For instant search:
a textbox where you actually type. its name "Finder"
a textbox where you save the searching string temporarily. its name "Search".
a listbox where you show result. its name "lstResults"
(NB: also to mention, the query, uses the textbox named "Search"; which is our temporary textbox.)
code:
Private Sub Finder_Change() ' "on change" event of "Finder" box
Me.search = Me.Finder.Text ' save the typed text in another textbox and use from there for query (otherwise it might not work)
Me.lstResults.Requery ' update listbox "lstResults" on any change
End Sub