How To Stop A Form From Closing After Validating A Field
Jun 12, 2006
Hi,
I've got a close button on my form as follows:
Private Sub exitForm_Click()
DoCmd.Close
End Sub
In the beforeupdate event of the form I have have following code to validate the forms fields:
Private Sub Form_BeforeUpdate(Cancel As Integer)
'Place an asterisk (*) in the Tag Property of the text boxes you wish to validate.
'Then in the BeforeUpdate Event of the form, copy/paste the following:
Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control
nl = vbNewLine & vbNewLine
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical + vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next
End Sub
This validation works fine up to a point. If I try to close the form and a required field is empty, I get a message box asking me to fill in the data. The problem is that when I click ok to the message, it shuts down the form, so I have to re-enter all the data again. My question is how do I prevent the form from closing after clicking ok on the message box?
I tried putting the following code in the form unload procedure, but although it does generate the correct message, it doesnt stop the form from closing.
Private Sub Form_Unload(Cancel As Integer) If [Starttime] > 0 And [Admin_time] = 0 Then response = MsgBox("Please click on the stop button to stop the clock") Exit Sub End If end sub
If the above condition holds, I want the user to click on the stop button before they close down the form. I'm guessing that the form is already commited to closing before the unload event? :o
I have two tables in an Access Database- One is the chart of accounts, and the other is the actual entries, with the common field being ID. Chart of accounts is set up something like this-
ID Description 1000 Customer Revenue 2000 Office Expenses 2100 Paper
That kind of thing. Anyway, I want to set up the ID field in the entries table so that it is basically a pull down box that only allows you to enter one of the IDs listed in the Chart of Accounts. Any way to do this? I'd really hate to have to write a quick separate program to do this.
i have a database am working on in access 2010. I want a field to take its value based on another field's combo.Example;i have a checkbox field called "Loan" "Yes/No" and i have another textbox filed "Status" (which will hold eligibility). I want the Status to read Eligible when the checkbox is No and Non-eligible when its Yes
I am trying to put in a validation rule so that data in one field cannot be less than that in another. e.g. i want to make a validation rule so that a phone call end time cannot be before the phone call start time. Can anyone help???
This may be a little difficult to explain but here goes:
I have a table in access 97 with the field Account. An account name is usually written as 0000000_A, but it MAY also have a secondary account and is usually written with the same first series of digits but ends in B e.g. 0000000_B. Therefore, we could have an account 0023654_A and 0023654_B.
Now for each account name there is another field called Items which are chosen from a combo box on a form (item 1, item 2, item 3, item 4, item 5, item 6, item 7 and item 8). An account may have more than one item.
Now if account B exists then it MUST contain the same entries as account A but can also have additional items. So we could have a situation where:
Now what I am trying to implement is that if the core items (these are those that are contained in both - which would be item 1, item 2, item 3 and item 4 in the above example) are changed in either accounts making them not equal then a warning message should come up stating so. IT IS IMPERATIVE THAT THE CORE ITEMS ARE PRESENT IN BOTH.
I have some experience in Access 97, but unsure of how to tackle this. Could somebody please guide me ???
Validating field from a query. I have a table with a field that has a value number that I need to validate that that number exist in another table in a field
Table1.field1 Number Table2.field1 number
So let's say a have in table2.field1 the list 1 2 3 4 5 8
In table1.field1 I need to validate that the number I enter is present in table2.field1 so 1 would be ok but 6 invalid and it can't be a from list statement because I need the person to enter a number and get no error or get invalid number.
i have two forms with a command button on the first which opens the other...
is there a way of getting this button to validate the primary key (i.e. make sure its not a duplicate) before it trys to open the next form??
i've tried to call the next record function (as the button seems not allow the next record to continue) but all this has done is delete the record and open the new form rather than stopping the action...
I have a form which allows an individual to add a cylinder record, i would like to validate the form in the sense that if the cylinder has already been added into the table it cannot be added again. So once the cylindernumber has been typed within the text field and the add button is clicked, the cylinder number typed is checked against the table if it has already been added a meesage should appear not allowing the user to enter a clyinder that is already added. I ahve tried this with the following code (the coding in red was already generated by adding a command button "add" to the form. The message i am recieving when the cylinder number in the textbox matches the cylinder number within the table is "you cant go to the specific record". I think my coding must be in the wrong order and IDEAS!!!!!
MyCode: Private Sub Command39_Click() On Error GoTo Err_Command39_Click
when i change records, my subform that has a memo field, shows the memo field data is "highlighted". i can make a mistake and hit any keyboard key and the memo field data will get erased.
is there a way to prevent such a problem?
if not, is there a work around. eg, hide the memo field until needed for more input? ideas on both issues?
I have a form that requires a specific combo box selection completed prior to closing or it doesn't register the field ID# in the table or display the payment in the payment query. When I click on the accept button on the form, if [PaymentMethodID] is blank, I want to put up a msg box informing me to "choose a Payment Method", then return the cursor to [PaymentMethodID] field to allow the user to make the appropriate selection.How would I write that in VBA.
I get an error message when I try to change either of these Code fields from size 10 to 16 characters
Error message - you cannot change the data type of field size. It is part of one or more relationships. Te change the data type of this field, first delete its relationships in the relationships window.
I deleted all relationships in the relationships window. But I still get the same error message.
If I click on relationship window > "All realtionships", Access reproduces all my relationships in the relationship window. Somehow it knows them. I deleted them all again and saved. But I still get the same error message.
I have a form ftTrades which generates automatically when I click on table tTrade and create a form.
I deleted this form ftTrades. But I still get the same error message.
I have an order form that auto-populates details from the customer table into adderss and contact controls. But I don't want it to auto-populate when the underlying order table already has data in the related fields. Here is the code I use on Load form event...
====== Private Sub Form_Load() 'Use this version if the ID is a number Dim x As Variant Dim strControl As String Dim lngID As Long 'If parameters exist, use them If Len(Me.OpenArgs) > 0 Then
I have a Combo box which contains a set of numbers divided by "."
e.g.: 1.342567.24433
When looking for a certain code, I would like to use the number pad to enter the first few e.g. 1.34 to move to the Position. In Previous Versions of Access I had a function on the key
If KeyAscii = 44 Then DoCmd.CancelEvent 'stop the comma keypress being updated SendKeys ".", True 'put a full stop in the field End If
This worked fine, but in Access 7 and up, it turns off the number lock.
Is there an alternative to sendkeys to achieve the above or a way of directly turning the number lock on again?
I am using Access 2010 - Version 14.0.61.29.5000 (32-bit)
I am building a custom Export Wizard to export data to Excel using the Report Wizard for the basic ideas.
All I am trying to do is have a [Back] button on a form to open another form and close the current form.
Private Sub cmdBack_Click() DoCmd.OpenForm "frm_ExportWizardPage2", , , , , , Nz(Me.OpenArgs) DoCmd.Close acForm, "frm_ExportWizardPage3" End Sub
The new form is opening but then the current form is not closing. All forms are the same size, shape and positioned centrally although this should not make any difference.
Could this be anything to do with which form has the focus when I open the new form?
How do you prevent access from adding a new record when u input data in the current record or tab to the next field? My database is set up to open with a form where the user picks his name and then a week ending date once that is complete u open a new form where the name and date auto populate along with other fields to fill out such as job charge, charge type , times charged for each day of the week. But I don't want access to create a new record everytime the person inputs or tabs..... .
I have a bound form with a few fields. I would like to run a sub after some of these fields are modified, but not others. I would like it to run only once, after user finishes his work on the form.
When using the Unload event - the form on the form is not there anymore (or did I get something wrong?).
Is there a way to trap Data on form just before the form closes?
This is related to some advice I received some time ago <a href="http://www.access-programmers.co.uk/forums/showthread.php?t=64818">here<a>
I am runnign some code, it performs some applications, then, if a Count Field is greater than 1, a form opens as a dialog, displaying some information:
Dim A As String A = "NewForm" If Me.[Count] > 0 Then DoCmd.OpenForm A, acNormal, , , , acDialog End If If Me.Continue = "No" Then Exit Sub
End If
The new form opens, and you can either view it, and click continue, which will run the rest of the code, or you can click quit, which updates Me.Continue to "No", and then it exits.
The problem is, when it exits the Sub, it doesn't really stop the code, it just leaves things kind of hanging. When I try to run the code again, I get:
3321 - The database engine could not lock the table 'NewTable' becuase it is already in use by another person or process.
Another symptom - The original form where I click to start running the code, has two date fields. Normally, when this form opens, they are blank, but when I exit the sub, as above, the previous dates I entered are still sitting there.
When a form is open you can also see on the task bar that the form is open If the user right clicks on the form(on the task bar ) they get the following options minimize , maximise , close.
Does any one know how I can stop the user selecting Close please ?
when i close the form all the fileds in the forms gets saved to the table. i want it should pop up before closing to save it or not. i have a button on the form to save the fields in the table. which has code