How to stop access trying to save record when you exit - vba

I have a form that isn't for adding or deleting , just checking if a record is in the database. Every time I try to close the form access try's to save the record to the database and then throws an error because there is already a record of the same values in the database.
How do I stop access from trying to save the record? I have tried Me.Dirty = false and Call Me.Undo in the onClose event. But none of these seem to work , any other ideas ?

As per comments above, adjusting the properties of the form to make it "read-only" (e.g., Data Entry, Allow Additions, Allow Deletions, and Allow Edits all set to False) solved the problem.

Related

Button to check for missing values in a MainForm and Subforms in MS Access

New to Access (still), have only basic VBA skills.
I've got 3 subforms (subfrm_PackingSteps1 , subfrm_MetalDetection and subfrm_Weights - the first 2 are continuous and the other one is single form) within a main form (frm_daily_packing_record) that users go through and input data. The user should be able to input data in no particular order, and only at the end there would be a button to confirm that the user is ready to save this form.
I'd like to have this button on the main form that checks each control (in main form and subforms) for empty values. I found and adjusted a code to check the recordset of one of the continuous forms (see below), but I can't figure out:
how to include a code that checks each control instead of manually adding all of them (I've used a function before that utilises the Tag property, but can't add it to this)
how to keep the button in the main form while checking the controls/recordsets in the other subforms.
Thanks in advance.
Private Sub ConfirmBtn_Click()
Dim blnSuccess As Boolean
blnSuccess = True
Me.Recordset.MoveFirst
Do While Not Me.Recordset.EOF
If IsNull(Me.pc) Or IsNull(Me.InnerP) Then
blnSuccess = False
Exit Do
End If
Me.Recordset.MoveNext
Loop
If blnSuccess = True Then
MsgBox "You may proceed to save this record"
Else
MsgBox "You still have some empty fields to fill in!", vbCritical + vbOKOnly, "Empty Fields!"
End If
End Sub
I personally don't like this because it is too code-heavy and can easily break when the form is modified.
Instead may I suggest doing it slightly different, for each field have vba code .ondirty or .onupdate and do the validation checking right as the user is actually on that field.
This has 2 benefits, it is creating the validation when you are creating each form field and it STOPS the user right when their first mistake or bad data is entered. The last thing I want is to enter 50 fields, scroll to the bottom, submit fails then scroll back and try to find where the mistake was. If validation is done while the user it doing the actual data entry, when you get to the bottom you should have valid data and the submit should succeed without further testing.
Less code to debug and timely messages to the user if an error is caught!

Why does MsAccess Record entry only Update (and show in table) after form close and reopen?

Some Background:
I have a database that acts as a ledger (keeps tabs on current payments).
1:Payments for each customer are stored in one table (PaymentTbl),
2:I have another table (TermAgreementTbl) that holds information on the agreed terms of the service
3: My last table (PdUpToTbl) takes the payment information as well as term agreements information from the two other tables and calculates/displays the information in a clearer manner. One of the ways it will do this, is by deleting the last record in PdUpToTbl, and replacing it, or by just adding a new record (case dependent).
Now MY Issue:
I have a form for my TermAgreementTbl that has a subform showing the relevant PdUpToTbl.
A button opens a pop-up form to enter a new payment and update the related PdUpTotbl.
Everything in the back end is functional, however after I enter a new payment (and save and close the pop-up payment form), NO new record is shown in my PdUpToTbl Subform. Instead it shows something like (some irrelevant info redacted):
For the new record to display properly, I have to close the entire form, and reopen it. There has got to be a way to get around this through vba with some code, right?
Thank you for taking the time.
Edit 1:
By the way, after I perform A LOT of vba code, I use this to enter my record:
With pdUpToRS
.AddNew
![DatePaid] = NewRecordSet.Fields("DatePaid").Value
![Amount] = Amount
![AppliedAmount] = AppliedAmount
![OnAcct] = OnAcct
![AllPdUpTo] = AllPdUpTo
![RemainBalDue] = RemainBalDue
![PdUpToString] = PdUpToString
![PaymentType] = NewRecordSet.Fields("PaymentType").Value
![PaymentNumber] = PaymentNumber
![ID] = NewRecordSet.Fields("ID").Value
![PmntTblID] = PmntTblID
![BdCk] = BdCk
![Late] = Lte
![ApplyDiscount] = ApplyDiscount
![ForgetUnderage] = ForgetUnder
![ForgetOverage] = ForgetOver
![Note] = Note
.Update
End With
Update using requery
I have tried to Requery using:
Forms![MainForm]![Subform].Requery
But it gives me the error:
2118 - - - You must save the current field before you run the Requery action.
And if I add the save line:
DoCmd.RunCommand acCmdSaveRecord
Forms![MainForm]![Subform].Requery
I get the resulting error:
2046 - - - The command or action 'SaveRecord' isn't available now.
Ok, the docmd "menu" options to save a record?
They OFTEN run on the form that has the current focus - so often, then the form you want to save is not the one you expected.
I suggest you replace this:
DoCmd.RunCommand acCmdSaveRecord
with
if me.Dirty then me.Dirty = false
Now, above above "me" is is the current form WHERE that code is running, not some form that might happen to have the focus.
Now, as for a form "requery" (to refresh without close then re-open)?
Again, assuming the above just did the save of the data, then to force a re-load of the current form (again, the form in which the code is running), then:
me.Requery
In fact, if you did not have multiple sub-forms, then a me.refresh would probably work (and a me.Refresh will also save the current record).
So, while the if me.dirty = true then me.dirty = false is about the best way to save the current reocrd in the current form where the code is running?
It is a question of where your code is running, and often when the code is running.
In place of the me.dirty = false, you can also do this, but it often will cause a lot more flicker and refreshing then you want.
But, the shortest code to ensure a save of the forms data, and then requery (same as form close then open), would thus be this:
Me.Refresh
me.Requery
However, often issues can arise if you have some dialog form open - so perhaps a closer look at how your code is updating is often imporant.
But, a me.Requery will re-load everything, but you of course want to ensure you force record saves of the data first.
I think the Requery function will serve you well.
https://learn.microsoft.com/en-us/office/vba/api/access.subform.requery

Copy Value from combo list on login screen to username field on new form

Newer to access...I am trying to copy the value selected from the combobox on the log in screen to a field on the new form that opens after successful login.
The field I need copied is called cmoemployee on the Login form.I need that to go to the username field on the mainframe form.
Any help would be greatly appreciated! I have tried many different ways but I can't seem to get it.
Haha,
I actually know what you mean here. This can be a complete pain as the variables are deleted when you open a new form In general it is better to show what you have tried so people are able to help you fix it. Anyway, I've tried to do this so I know how frustrating it can be. There are two main ways that this can be done. You can open the form with openargs (I do not think this is a good option for you as you will have to have the openargs constantly transferring from one form to the next and will be cleared if one form misses them.
A better option would be to use tempvars which are not deleted. This is the code I used to do something similar:
I am going to assume your combobox is two columns (but only displays one) and the bound column is the primary key to an employee table. In which case this would work (or you can modify it to meet your needs)
Log in:
'Check Password (or however you check it
If StrComp(txt_Password, DLookup("[Password]", "tbl_Employee", "[ID] = " & cmoemployee), 0) = 0 Then
TempVars.Add "MyEmpID", cmoemployee.Value 'TempVars will store the combobox value
Else
Msgbox "Incorrect Password Please try again"
End If
On Load event of mainfraim form:
[UserName] = TempVars!MyEmpID
That should get you started, so you can build the rest. Let me know if you have any questions.

VB Msgbox w/ SQL call

Just a quick VB question -
I'm using VB6, and I just need to add an If...Then statement to prompt the user when they are overwritting a field previously populated in SQL. This will keep additional users from updating the field multiple times without knowing that I updated it already. I'm just unsure of the order of this belongs in. Previously, it was in this order:
On button click, test connection to sql database, and update field userName with user1's name if it is currently empty.
close sql connection
After adding my If...Then, it's something more like this:
On button click, test connection, and update field userName with user1's name IF it is empty.
Once updated, update 'updated' boolean field(I added a field to keep track each time there is an sql update) to True.
Added IF statement before End IF on first statement, stating that IF 'updated' = True, then display a YES/NO Msgbox prompting user2 to make a decision on whether or not they want to continue to update the userName field for the second time, and overwrite user1's name, then End If.
End If on the first updating statement.
close sql connection.
End goal is to prevent user2 from overwriting user1's update immediately without receiving some type of prompt that user1 has already updated it. I'm just not sure if my code should surround the initial updating IF..THEN, or be put inside or it where it initally checks if the field is empty. Any help would be great.
I would have provided the actual code, but it's proprietary.
I think this is what you have in mind?
(connect to SQL, get field)
If strSQLField = "" Then
If fUpdated = True Then
nReturn = MsgBox "Do you want to overwrite the field?", vbYesNo
If nReturn = vbYes Then
(do overwrite code here)
End If
Else
(do normal write code here)
End If
End If
(close connection)

Runtime error '2105' "You can't go to the specified record"

I have a button on a form whose purpose is to add a new record to the underlying
table.
The OnClick Event code for the button looks like
me.dirty = false
if me.NewRecord then
msgBox("new record")
else
msgBox("not new record")
end if
doCmd.goToRecord record := acNewRecord
The message box is needed to make sure that I am not already operating on a new record.
When I click the button, I get a Runtime error 2105 "You can't go to the specified record"
Does someone know why this is?
Rene
Never Mind, I found the problem
I should have used
doCmd.goToRecord record := acNewRec
instead of
doCmd.goToRecord record := acNewRecord
Yet, why access won't tell me that it doesn't know about acNewRecord....
Edit: Of course David was right: I haven't set OPTION EXPLICIT.
I was able to resolve the 2105 issue by selecting a unique record identifier for the underlying linked table that was the record source for the MS Access form.
i also have faced this issue and in my case this belowed solution worked well
try this ,
if form doesn't allow you to add new record then make sure that
before the form opensup all the other forms that also used tha same table as datasource are needs to be close first.
double click on the up left point on the small black squere in the form to have the main properties of the form, then go to the data tap and click the Record source button, you will see that the form is linked to another table which is prevent the form to add new records, remove that table, and I hope that will solve your problem.
Salam...