Using MsgBox without pausing the application - ms-access

I need to display a message to the user. When I do this using MsgBox, the program stops until the user clicks the box away. I'd like to know if there's a way to open the MsgBox without pausing the program.

Sounds like you're not expecting any user input from the MsgBox. In this case, depending on your application, the StatusBar may be an adequate substitute.
In Excel this is easy:
Application.StatusBar = "Please be patient..."
Application.StatusBar = iDone & " of " & iTotal & " items done."
To clear the StatusBar when done:
Application.StatusBar = False
In Access, the syntax is a tiny bit more convoluted:
Temp = SysCmd(acSysCmdSetStatus, "Hey, look at me!") ' Puts out your message
Temp = SysCmd(acSysCmdClearStatus) ' Clears StatusBar

As far as I've ever been able to discover, the answer is you can't. The work-around is a custom form that serves as a dialog box.
See http://www.mvps.org/access/forms/frm0046.htm (not precisely your question, but applicable).

MsgBox is modal (meaning the window comes up and halts execution of code until it is cleared). As other posters/commenters have mentioned - your alternative is to write your own version of a popup that is not modal. Not really worth the effort unless you really need it that way.

In the VB editor: Select Insert menu UserForm.
In the Toolbox select TextBox: Drag a rectangle in the UserForm and type your text into it.
Right click on the UserForm and select Properties.
In the ShowModal property: Select False.
In your VBA module enter UserForm1.Show where you want to turn it on and UserForm1.Hide where you want to turn it off.
UserForm1 is mine, of course use the appropriate name for the form you created.

Create a Form instead. I created a small form that only has a text box that says "Working, Please Wait". When needed I open the form, as a pop-up (docmd openform "form name"), usually just before starting some operation that is going to take some time to complete. When the work completes I close the form (docmd close acform "form name"). This does not stop the program but does provide a "Message" to the user.

You can use WScript's Popup method. Here's the full details including sample code: http://msdn.microsoft.com/en-us/library/x83z1d9f%28v=vs.85%29.aspx

I believe you first need to evaluate if you really need a msgbox to pops-up and keep your code running.
The msgbox functionality (as already stated) is modal and you cannot 'bypass' it. However, you can create a form (similar to the msgbox), set this form as 'not Modal' and call the code to show this form. The code workflow goes on. Tested and works in Excel.

You can use this. It worked for me to create a dialog for outlook:
Dim myMessage as String
Dim myDelay as Integer
myMessage = "This message will self destruct in 8 seconds!"
myDelay = 8
Shell "msg /TIME:" & myDelay & " " & Environ("Username") & " " & myMessage

Related

DCOUNT result not being passed to tab control caption (error)

I've create a form with a tab control in MS-Access.
On one of the tabs I have 2 subform controls showing records from the same table. This table has a field I've called "Status" and can have an entry of either "Open" or "Closed"; the 2 subforms simply show these 2 options filtered as separate groups on the tab control.
I then have a button underneath each of the subforms; the user selects (clicks into) one of the records in either of the subforms and then the button will open a new "Edit" form at the selected record in subform so they can see more information and also toggle the "Status" field to "Open" or "Closed".
When the user presses the "Save and Close" button this saves the changes, closes the "Edit" form, and re-queries the 2 subforms.
What I'm trying to do is get a count of the number "Open" records still showing after the "Edit" form is closed. The count number is then to be passed in to the tab caption for this tab so users can see at a glance how many "open" records are against the record on this main form.
Here's my code so far:
Private Sub cmdAdminIssue_Edit_SaveClose_Click()
' save and close currently open edit form
DoCmd.Close acForm, "tblAdminIssue_Edit", acSaveYes
' requery the subforms to show the effect of the edits made via the edit form
Forms![tblJobs]![tblAdminIssue_Sub_Open].Form.Requery
Forms![tblJobs]![tblAdminIssue_Sub_Closed].Form.Requery
' count number of records still open and pass the number through to the tab control caption property
Dim AdIssOpenCount As Long
AdIssOpenCount = DCount("JobID", "qryAdminIssue_Open", "JobID = '" & Me![JobID] & "'")
Forms![tblJobs]![tab_AdIssues].Caption = "Admin Issues (" & AdIssOpenCount & ")"
End Sub
The above code triggers the error: "The expression you entered refers to an object that is closed or doesn't exist", highlighting my DCount expression in debug.
However I can get the DCount to work independently of the other code above it; they seem to be interfering with each other, but I don't quite understand how.
Referring to the error message above it seems to be suggesting that DCount needs the query I referenced to open in order to run... this doesn't make much sense to me as I thought these kinds of functions do not require you to explicitly open in code an object it's trying to get data from... I'm probably misinterpreting what this error actually means though.
Any explanation of the error and a possible workaround would be much appreciated. Thanks.
While you've clearly done your best to explain the situation it seems pretty complicated.
I'm going to try and break it down a bit, and suggest something you might do.
"I've create a form with a tab control in MS-Access.
On one of the tabs I have 2 subform controls showing records from the same table.
This table has a field I've called "Status" and can have an entry of either "Open"
or "Closed"; the 2 subforms simply show these 2 options filtered as separate groups on
the tab control."
So, you have a form, with 2 subforms. That they're on a tab page shouldnt really be relevant. I'm going to reference the form as frm & the subforms as sf_o & sf_c.
Button underneath each subform (opening the detail form with a double-click might be nicer) - the "save & close" button is presumably in the popup screen; This button is currently performing the save of it's own data, and doing everything else as well.
This is very messy, as it means that the contents of frm cannot be modified without having to make changes to the logic of the save/close button as well. A better approach would be to separate the logic for refreshing the main form from the editting form using a public method in the main form (frm):
Public Sub RefreshData()
sf_o.Form.Requery
sf_c.Form.Requery
'other logic
End Sub
then, you have:
Private sub cmdAdminIssue_Edit_SaveClose_Click()
'save & close logic...
Forms(frm).RefreshData
End sub
Okay now, the other logic. You want to find out how many records are now "Open" and put this information somewhere. In this case, somewhere is a tabpage caption, but this isnt really material.
I never use DCount (DLookup etc) as it is very slow. An alternative (and in this case, much simpler) method would be to use the forms own properties:
tab_AdIssues.Caption = "Admin Issues (" & trim(sf_o.Form.RecordsetClone.RecordCount) & ")"
How's that? Then you dont need to try and figure out what's gone wrong in all that pre-compiled logic which is microsoft access.
Hope this helps

After creating a new record, I can't get my form to display it in Access 2010

StackOverflow. I have an issue I've been grappling with for too long. Currently, I have a form that displays consortium data, with a button that says "Create new consortium." When I click it, it shows a pop up window that allows you to enter the name, as displayed in the image below. Almost everything works fine, except I can't get it to display the new record after I create it. See my code below--you'll get a sense of what I'm trying to do.
By the way, if it matters, the "Consortium name" combo box allows you to select records. I don't see how it could throw my update off, but I thought I should include that information.
Here's the form:
Here's the code:
Interestingly, StackOverflow doesn't seem to indent the code properly after making an edit--I was able to do it successfully with a new test post. Oh well. I'm a noob to this as well:
Private Sub CreateConsortiumButton_Click()
If IsNull(Me.ConsortiumNameText) Or Me.ConsortiumNameText = "" Then ''insert better validation here
MsgBox "Please enter a valid consortium name."
Else
'' Create a new Consortium record with the ConsortiumNameText field on the popup window
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("Consortium")
rst.AddNew
rst("Consortium name").Value = Me.ConsortiumNameText
ConsortiumID = rst("ConsortiumID").Value
rst.Update
'' Now, create a corresponding record in the ResearchContributions table
Set rst = CurrentDb.OpenRecordset("ResearchContributions")
rst.AddNew
rst("ConsortiumID").Value = ConsortiumID
rst.Update
MsgBox "Consortium " & Me.ConsortiumNameText & " successfully created."
'' Change the form's combo box to reflect the new record
Forms!Main!NavigationSubform!ConsortiumNameCombobox = Me.ConsortiumNameText
'' Close popup
DoCmd.Close
'' Attempt to set the record on the main form to the newly created one - NOT WORKING!
DoCmd.OpenForm "Main", acNormal, , "[ConsortiumID] = " & ConsortiumID
End If
End Sub
I think part of the issue may have to do with the fact that I've placed the "Consortium Form" form, which is based around a tab control, into the "Main" form. Perhaps because I'm trying to open the Main form with a certain ConsortiumID, that ConsortiumID doesn't apply to Consortium Form. It appears as though I can't link the master with its child, but this hasn't been necessary in the past. Please forgive me if the concepts sound vague in confused, for it is only because the concepts are vague and confused as of yet in my mind--this is my first Access project. Anyone have any tips?
Okay, I think I figured it out. I've added the following code:
DoCmd.OpenForm "Main"
DoCmd.Requery
DoCmd.SearchForRecord , , acFirst, "[ConsortiumID] = " & ConsortiumID
First, open the form, then requery in able to register the fact that the new item is in the database, then use SearchForRecord to open it up. Neato. Thanks, everyone.

Error 2448: Can't assign a value to this object ... Why not?

Private Sub Form_Open(Cancel As Integer)
Me.Bathroom = Forms!frmBathrooms!ID
Me.txtBathInfo.Caption = "Bathroom Room Number: " &
DLookup("Room", "tblRooms", "ID = " &
DLookup("Room", "tblBathrooms", "ID = " & Me.Bathroom))
Me.RecordSource = "SELECT * FROM tblStalls WHERE Bathroom = " & Me.Bathroom
Me.Recordset.AddNew
End Sub
where Line 2 Me.Bathroom = Forms!frmBathrooms!ID is tripping the error.
Debugger says Me.Bathroom = 1, Forms!frmBathrooms!ID = 38. Basically I'm trying to automatically make this form's Bathroom field match that of the frmBathrooms form's ID. "Me" is a form for items in the bathroom; there can be many items.
How the hell is that throwing an error? I can't find anything dispite about an hour of searching around. I understand the message, but not how this could be throwing it?
The Bathroom field of the Item table is in the form's recordsource, etc. Ideas?
Had the same problem, but it had nought to do with VBA syntax.
I attempted to set a control (ordernumber) on a form.
This ordernumber had to be calculated, which i proceeded to do in VBA.
After calculation I would try to set the control (ordernumber) on the form. And that triggered an 2448 error at runtime.
Subsequently I discovered that in form design, i had set the control source to a calculation already. So run-time VBA would not allow me to set the control as well.
After removal of the calculation in the Control Source of the control in the form design --> Property Sheet window it worked fine.
I admit, really stupid mistake, but there you have it.
Hope this helps
--X--
For anyone else having the same problem, all I did was literally move lines 2 and 3 down to below Me.Recordset.AddNew (so that it changes source and adds a new record BEFORE changing the Me.Bathroom and caption). – user1394455
The main form has to be editable in order to change the drop down list, even in the form header area. (VBA was making it non-editable upon loading.)

access to oldValue for control in continous form generates error 3251 in beforeUpdate when a checkbox on form is updated

This is one of the stranger issues I have seen in MS Access. I have the following code in a continuous form:
Private Sub thisForm_BeforeUpdate(Cancel As Integer)
If Not Cancel Then
Debug.Print "pre-logging data changes..."
' here we need to doublecheck to see if any values changed.
' we simply iterate through the whole list, re-setting oldValue
' and newValue.
For Each control In thisForm.Section(acDetail).controls
If control.ControlType = acTextBox Or _
control.ControlType = acComboBox Or _
control.ControlType = acListBox Or _
control.ControlType = acOptionGroup Or _
control.ControlType = acCheckBox Then
Debug.Print control.Name
oldValues(control.Name) = control.oldValue
newValues(control.Name) = control.value
End If
Next
End If
End Sub
oldValues and newValues are Dictionary objects (although likely not related to the issue).
My form has 3 textbox controls, and a checkbox control. One of the text box controls is disabled, and is populated via the results of a simple inner join (to get the human readable name associated with a foreign key). The data source comes from the form's recordsource (no DLookup or anything is used).
If I edit one of the other two textbox controls, this code runs absolutely fine. HOWEVER, if I toggle the checkbox on the form, i get a runtime error 3251. In the watches window, I get the error again when i try to view the properties of "control". It shows the value of oldValue for the disabled control to be "Reserved Error".
If it did this consistently, I would think it was due to the control being disabled; but since it works without a problem when the other textboxes receive edits, and only breaks when the checkbox is toggled; I am stumped. I'm almost inclined to believe I found a bug in access, but I could use some extra input.
Anyone else every encounter an issue like this?
EDIT: Upon digging further, I found that in actuality only one of the 3 editable fields will not trigger this error. It holds string data. The other two controls hold a date value, and a yes/no value. Now I am even more confused.
i've got two ideas to that issue.
First one: If the RecordSource of your Form is an ODBC-Table thats linked to a SQL-Server then you should set a standard value for the CheckBox-Column. Otherwise it will try to set NULL to False and throw an error saying that somebody else edited the current record.
Second idea: Sometimes Access just has a little "hiccup" when it compiles the code. You could make a backup of your database and then try to decompile it using "C:\Program Files\Microsoft Office 2007\Office12\MSACCESS.EXE" "C:\yourFolder\yourDatabase.accdb" /decompile in the Run... Window (of course you have to insert the Path as it is on your machine). That often helps solving strange Problems.

Disabling msgbox in access

I am trying to make a small form in MS Access 2003 SP3, I use some function that some other people made and these function has msgbox in it. I would like to disable msgbox while I am running the form. Is it possible in Access to disable msgbox?
I created my finction called msgbox. Seems like its working. Thanks everyone for your help.
Public Function MsgBox(Prompt, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title, Optional HelpFile, Optional Context) As VbMsgBoxResult
If myProcedureisRunning then
VBA.MsgBox Prompt
else
debug.print prompt
endif
End Function
If in fact these message boxes are produced from VBA code, then comment them out. However, if they are Access generated, such as the message box when inserting or updating records, you need to use the command DoCmd.SetWarnings False in order to suppress them. Just make sure to turn warnings off only when needed, then turn them back on. Otherwise, ALL message boxes from Access will be off, even in "design mode".
Do a CTRL-F and find for MSGBOX and comment it. I guess that's the only way you can do it.
Press Alt+F11 to open the Visual Basic IDE, then press CTRL+F to search. Type msgbox into the find, select "Replace" and type'msgbox into the "replace with" box (note the apostrophe). This will comment out all msgbox statements in the project.