Access: trying to open a form and populate a textbox with the value from another form - vba

Okay, I'm obviously doing something wrong, but can't figure out what it is, and this should be pretty simple.
I have a button on a form that opens another form and populates a textbox in the second form with a value from the first form.
Here is my code:
Private Sub test_Click()
DoCmd.OpenForm "subfrm_EA_COMMENT_ADD"
Forms!subfrm_EA_COMMENT_ADD.txtUWI = Me.txtUWI
End Sub
This works just fine, however I want the form to open in a dialog window, so I modified the script to this:
Private Sub test_Click()
DoCmd.OpenForm "subfrm_EA_COMMENT_ADD", acNormal, , , acFormAdd, acDialog
Forms!subfrm_EA_COMMENT_ADD.txtUWI = Me.txtUWI
End Sub
Now I am getting a run-time error 2450.
I've searched around regarding this error, but can't find any answers that work.
Can anyone point me in the right direction as to what I'm missing?
Thanks!

acDialog parameter causes code execution of first form to suspend until second form is closed. So either remove the parameter or place code in second form to populate its own record.
Pass value with OpenArgs argument:
Private Sub test_Click()
DoCmd.OpenForm "subfrm_EA_COMMENT_ADD", acNormal, , , acFormAdd, acDialog, Me.txtUWI
End Sub
To read OpenArgs property, code behind second form, possibly in the Current event:
If Me.NewRecord Then Me.txtUWI = Me.OpenArgs

I found a work-around for this issue using a temp variable. I created a temp variable at the beginning of my Add Comment button, then set the unbound text box in the Comment dialog window to the value of the temp variable. This works like a charm and I can now open my windows in dialog instead of normal.
I just wanted to let others know in case they run into this issue.

Related

How to refer to a report's control via a form?

I have a report that can be opened from two different forms, and depending on from which form it is opened, it's title should be changed. For example, if I press the print button on the other form, then it refers to the report it's about to print and uses Label.Caption to change the title.
Does anyone have insight on this?
When you open the form, call it using the parameter OpenArgs (the last parameter):
DoCmd.OpenReport "YourReportName",,,,, Me.Name
Read that when the report is opened:
Private Sub Report_Load()
Me.Caption = Nz(Me.OpenArgs)
End Sub

How to navigate to a form based on a comb box selection in Microsoft Access

I'm creating a form for my Microsoft Access database. I have a combo box, and I need it to navigate to the form based on which item the user clicks.
How would I do this? I know it requires some VBA code, but none of the methods I have tried have worked thus far.
The form I am trying to navigate to is called "Forms_Reports"
My current code:
Private Sub Combo0_AfterUpdate()
If Me.Combo0.Value = 1 Then
DoCmd.OpenForm "Forms_Reports", acNormal
End If
End Sub
Your code is basically okay, but you can't combine it in a single line. Also, I'm guessing your form is really just named "Reports". You don't include the Form_ prefix displayed in the project window. Try this:
Private Sub Combo0_AfterUpdate()
If Me.Combo0.Value = 1 Then
DoCmd.OpenForm "Reports", acNormal
End If
End Sub

Row gets modified when form is opened in access vba

I need to prepare a screen that will work as a comments screen as used on Facebook. I have to prepare it on access form. Where User will enter the comments and the same should get stored in a table with PartNumber and ItemNumber. Also the recent Comments should be displayed on the same form.
I have prepared a form having Recordsource as my table where the comments needs to be saved. And written a click event where as user clicks the button the comments will get saved in table. But the prob is that without clicking that button if i type values in textbox and close the form still the table gets updated with value before click. Below is the code
Private Sub Post_cmnt_Click()
Me.RecordSource = "Part_GeneralPartComment"
Call FromForm_Add
End Sub
Access automatically commits pending changes when a bound form is closed. If you want to prevent that from happening you can add the following code as the On Close event handler for the form:
Private Sub Form_Close()
On Error GoTo Form_Close_error
DoCmd.RunCommand acCmdUndo
Exit Sub
Form_Close_error:
If Err.Number <> 2046 Then
' error was something other than "The command or action 'Undo' isn't available now."
Err.Raise Err.Number
End If
End Sub

VBA Listbox becomes unresponsive after first use

I have a VBA (Excel 2010) system which involves selecting an item from a listbox and then displaying it in another form. Here is a very simplified version of what happens.
' Part of frmForm1 code module
sub lstListbox_Click
dim MyEvent as string
dim i as integer
i=me.lstListbox.listindex
MyEvent=me.lstlistbox.list(i)
' Now show the item in the second form
Load frmForm2
me.hide
ThisWorkbook.LoadDataIntoForm2 (frmForm2, MyEvent)
frmForm2.show
unload frmForm2
me.show
end sub
The listbox accepts the click, and first the event (the event handler is giver above). Key parts of the event handler are:
Load the second form (to display the detail data)
Pass the second form as a UserForm parameter to a procedure (LoadDataIntoForm2)
Hide the host form (frmForm1) and show the second form (frmForm2)
When the second form processes an Exit click, the code looks like this:
' Part of frmForm2 code module
sub cmdExit_Click
me.hide
end sub
The first time round it works fine - but when I return to frmForm1 (in the tail end of the lstListBox_Click procedure), even though the rest of the form is operative, the listbox remains stubbornly unresponsive.
I've managed to abstract this down to a little demo system if that would help - the same behavior is seen there. (It's regular .xls file, but that seems not to be easily acceptable as an upload)
Has anyone seen this before? And does anyone have any ideas how I might get this to work the way I want it to?
Thanks,
Tony
The default for the .Show method is to make the form modal. Explicitly set it to modeless:
Sub lstListbox_Click
...
Me.Show vbModeless
End Sub

Access 2007 VBA - Rename report upon closing it?

I have a report that is dynamically generated depending on the button pressed on my main form, in order to change the filter, the query used, etc. I have DoCmd.Rename working to rename the report to the current (dynamic) report title. However, it appears that I cannot rename the report back to a generic name upon closing the report.
Using the Report_Close() event doesn't work; Access tells me the report is still open and therefore can't be closed. Using DoCmd.Close doesn't work either; I get Runtime error 2501 (The Close action was cancelled).
How can I rename this report after it's closed?
Are you saying that each time someone changes the settings and opens a report, you want to save that as a new report in Access?
I wouldn't recommend this.
If the dynamically changed stuff are just things like filter and query, why not always use the same report and set the RecordSource dynamically?
EDIT:
Okay, now I understand what you actually want to do.
You can set the Caption property of the report at runtime in code:
Private Sub Report_Open(Cancel As Integer)
Me.Caption = "Incidents By Assignee"
End Sub
You can also pass the text for the caption from your main form to the report:
Pass the text from the form in the OpenArgs parameter when opening the report:
DoCmd.OpenReport "YourReport", acViewNormal, , , , "Incidents By Assignee"
...and in the report, just set the Caption to OpenArgs if it's not empty:
Private Sub Report_Open(Cancel As Integer)
If Nz(Me.OpenArgs) > "" Then
Me.Caption = Me.OpenArgs
End If
End Sub