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

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

Related

MS-Access: How to refresh a form after data was updated?

I have a query shown in a form as a table. Additionally I have a button which opens another form where you can manipulate saved data. As soon as this form closes I would like to have the query in the other form to be updated by a macro. I tried couple of macro commands. Nothing worked. I thought I could use the requery macro with my subform as parameter but even that didn't work. What can I do?
Data gets only updated when I hit 'refresh all' but this should happen automatically.
I have ran in to all sorts of strange edge cases with forms containing subforms. What happens when the current record is removed?
This may seem overkill but if you want to be sure that all forms are up to date then you can use this on the parent form:
'#Description("Refresh the data on the form and all subforms")
Public Sub RefreshForm(ByVal theForm As Form)
On Error GoTo ErrorHandling
Echo False
RequeryInPlace theForm
theForm.Refresh
MoveToValidRecord theForm
Dim childForm As Control
For Each childForm In theForm.Controls
If TypeOf childForm Is SubForm Then
RequeryInPlace childForm.Form
childForm.Form.Refresh
MoveToValidRecord childForm.Form
End If
Next
ErrorHandling:
' Fail fast, ensure echo is always left on!
Echo True
End Sub
'#Description("Requery an entire form without losing the currently selected record")
Public Sub RequeryInPlace(ByVal theForm As Form)
Dim whereIam As Variant
With theForm
whereIam = .Form.Bookmark
.Form.Requery
.Form.Bookmark = whereIam
End With
End Sub
'#Description("Move cursor to a valid record")
Public Sub MoveToValidRecord(ByVal theForm As Form)
With theForm.Recordset
If .EOF And .BOF Then Exit Sub
If .EOF Then
.MoveLast
ElseIf .BOF Then
.MoveFirst
Else
.MoveNext
.MovePrevious
End If
End With
End Sub
Ok, the way to do this?
If you just editing data? (not adding new rows) to that grid?
Your code to launch the 2nd form will look like this:
me.Refresh - optional but REQUIRED if you do allow editing in the gride.
docmd.OpenForm "frmDetailsEdit",,,"ID = " & Me!ID,,acDialog
me.refresh - this will show any data changes you made in the 2nd dialog form
Note carefull:
You don't need the first me.Refresh UNLESS you allow edits in the data/grid display.
The acDialog will cause the code to WAIT/HALT until the user is done editing in the 2nd form.
The final me.refresh will update any data changes, and it will also keep the reocrd pointer on the same current row.
However, if your 2nd dialog form launched ALSO allows adding of records, then a me.refresh will NOT show the newly added records.
If you allow adding? Then you need to do a me.Requery on that last line in place of me.refresh. This will re-load the form based on its query, but you will ALSO lose the current position. You can if you wish re-position the record pointer/location if you need to, but we don't know if you need this ability as of yet.
the query is not being updated in background unless you are opening the form which calls the Query to run , an alternative is to request the query to run again to update the form as follows
Form.Requery
if your form is having a sub form then you need to address the sub form as well

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

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.

Add data into table on button click

I need lots of help with my VB betting app project. Right now I'm stuck on the syntax for a submit button that sends user input from the textboxes in the vb form to the ms access table.
This is my code:
private sub command1_Click()
Dim rs As Recordset set rs=currentdb.openrecordset("tblGame_information") with rs .AddNew !awb=Me.txt_awb .Update
End With
End Sub
And after that, clicking the add button the error message says:
method or data member not found
The Private Sub Command1_Click () is highlited yellow
sure textbox txt_awb in the form and the field awb in the table exists (typo)? Always use
Option Explicit
on top of form or module (this prevents typing errors in identifiers).
Renamed Command1? Then you have to rename the click event too.

Programming VBA in an Outlook form

I created my own Outlook form to use it as standard surface to enter certain orders instead of the normal message form. The creation, editing and sending works perfectly fine and in the next step I want to insert some code via VBA.
My problem is that I can´t access the objects of my form in the VBA editor. E.g. I want to show a message box when a certain checkbox is checked. According code would be:
Sub example()
If CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
When I run the code I get the error that the object could not be found. The same goes for every other object, like textboxes or labels etc.
I guess the solution is pretty simple, like putting Item. or sth. like that in front of each object. But so far I wasn't able to find the solution.
I´m using Outlook 2010.
I know this is a year too late but you'll want to do something like this example below. It's kinda a work around but you can get whatever value was selected.
Sub ComboBox1_Click()
Set objPage = Item.GetInspector.ModifiedFormPages("Message")
Set Control = objPage.Controls("ComboBox1")
MsgBox "The value in the " & Control.Name & _
"control has changed to " & Control.Value & "."
End Sub
You should be able to get the value, just get a handle on the object you want using the Inspector
The following is an excerpt from here
When you use a custom form, Outlook only supports the Click event for
controls. This is a natural choice for buttons but not optimal for
controls like the combo box. You write the code by inserting it into a
form’s VBScript editor. You need to have the Outlook form open in the
Form Designer and click the View Code button found in the Form group
of the Developer tab.
Sub CheckBox1_Click()
msgbox "Hello World"
End Sub
The code page is fairly minimal with no syntax highlighting. I just tried this now and it does work. Dont forget to Publish your form to pick up the new changes.
I know this is almost 6 years late but, in VB and VBA, simply start with the form name. (And if that doesn't work, just keep going up a parent object and you'll get there.) So, your code becomes:
Sub example()
If MYFORMNAME.CheckBox1.Value = True Then
MsgBox("Checkbox 1 is checked.")
End If
End Sub
Of course, after typing "MYFORMNAME." you'll know if it will work because typomatic will kick in when the system recognizes "MYFORMNAME" after you hit the period.

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