MS Access does not add new Entry - vba

So I have created a Table and a Form, which allows you to Save New Record to the table, by entering some fields.
Everything works fine, except that while clicking "Save New Record" button, the new entry is NOT saved, but it Edits the previous entry!
So no more then 1 record is inside the Primary Table.
Could it be something with Relationships ?
I have two tables:
PrimaryTable
ContactsTable
Both have the same name as Primary Key and are connected via that Key (inside relationships).
If you need more information or some screenshtos, pleaese let me know in comments!
CODE FOR SAVE NEW RECORD:
Private Sub onSaveBtn_Click()
DoCmd.RunCommand acCmdSaveRecord
DoCmd.Close acForm, Me.Name
End Sub
Thanks,
David

Since you created your form through the wizard it is likely a bound form. This means that as you edit information in the form you are editing the records directly. When you open your form if there is already data populated then the command you have included in your button DoCmd.RunCommand acCmdSaveRecord is going to save the information displayed on the form to whatever record was open.
There are a few ways you can deal with this issue. At the bottom of your form you may have navigation buttons and one with a * icon beside it. This indicates create a new record and will clear the form and move it to a blank record when pressed.
You can also add a button on your form with the following code that will do the same thing.
DoCmd.GoToRecord , , acNewRec
Another option you have is to create an unbound form that will allow you to completely control how your users navigate your data.

Related

MS Access one form to select table values and use to populate multiple fields on other form?

I have a table called GL_Account and IM_Productline
The table IM_Productline has various fields that need to be populated with a value from the field GL_Account.AccountKey (i.e. IM_ProductLine.InventoryAcctkey and IM_ProductLine.CostOfGoodsSoldAcctKey)
To populate the IM_ProductLine table I made a form "Product Line Maintenance" with all the fields. To populate the field IM_ProductLine.InventoryAcctkey I put a (magnifying glass) button behind the field with the following code:
Private Sub CMD_Select_GL_Account_Click()
Me.Refresh
If IsNull(Select_ProductType) Then
'do nothing
Else
Forms![Product Line Maintenance].InventoryAcctkey = Me.SelectGLAccountKey.Column(0)
Forms![Product Line Maintenance].Refresh
End If
DoCmd.Close
End Sub
So the button opens a form Called "Select GL Account" with a combo box that enable to SELECT GL_Account.AccountKey, GL_Account.Account, GL_Account.AccountDesc
FROM GL_Account; and when the OK button is clicked it writes the value from GL_Account.AccountKey to IM_ProductLine.InventoryAcctkey, closes the form "Select GL Account" and then refreshes the form "Product Line Maintenance" so the account number and description become visible for the user.
This all work fine but here's my question:
Now rather than creating a new form for every account field I need to populate (i.e. "Select Inventory GL Account" select "Cost Of Goods Sold GL Account" etc) I'd prefer to use the form "Select GL Account" to select and populate the 11 different account fields. So behind each xxxAcctkeyfield on form "Product Line Maintenance" is a (magnifying glass) button that when clicked pulls up the form "Select GL Account" and when "OK" is clicked it writes the selected AccountKey to the correct field on form "Product Line Maintenance"?
I'd greatly appreciate anyone's efforts to understand what I am trying to explain and point me in the right direction.
Ok, there is the issue that all 11 fields should not require to be "copied" since you have a relational database (you would ONLY store the row PK ID of that selection in the current report. (a so called FK (foreign key) value). That way, say you want to change the choice? Well then you could pop up that form - search + select the one record with all that information, and then upon return ONLY store the one value.
So, I would give some thoughts to the above - you want to leverage the relational database features. And as a result, you don't need to "copy" all that data. This is not much different then say creating a invoice. I can create the new invoice, but all of the address information, and the customer that this ONE invoice belongs to? Well, that is one column with a FK value that points to the customer. Once I select that one customer, then display of the customer name + address can be say a sub form or some such - but no need exists to "copy" that information. It would also means with near zero code, you could move a invoice between customers!!! - (just change the one fk column with to the new/different customer ID (PK) value.
Now, back to the question at a hand?
You can certainly pop up a form, let the user select, enter, pick and do whatever. And THEN you can have the calling code grab + pick out the values from that form.
The way you do this? It involves a not too wide known trick.
The code that calls the form can simply open that form as a dialog form. This will HALT the calling code, the user does whatever, and when done the calling code will THEN continue. Not only does the calling code continue, but it can get/grab/pull/take any values from that pop up form WIHOUT having to use global vars.
The approach is thus thus:
dim strF as string
strF = "frmPopAskInfo"
docmd.OpenForm strF,,,,,,acDialog
' above code waits for user input
if application.AllForms(strF).IsLoaded = true then
' user did not cancel, get values from form
me!AccountNo = forms(strf)!AccountNumber
etc. etc. etc.
docmd.Close acForm,strF
end if
Now the only other issue? Well, the "ok" button on the popup for DOES NOT close the form, what it does is set visible = False. This will kick the form out of dialog mode.
me.Visible = False
So, if the user hits the cancel buttton (close form) or hits the X upprer right (close form), then the form will NOT be loaded when your calling code continues. But, if they hit OK button, then you don't close the form, but ONLY set visbile = false.
This allows the calling code to continue, you are free to get/grab/take values from that form, and then once done, you close the form.
So a form close in that popup = user canceled the form.
So, a popup form, and even a modal form? They do NOT halt the VBA calling code, but a acDialog form does!
You can thus place 2 or 5 little buttons that pops up this form, the user can pick/choose/select/enter values. When they hit ok, your calling code continues, and you are free to pull values from that form. So while all 3-4 buttons might pop up that form, each individual button launch of the form can have code that follows and updates the given control the pop button was placed beside.

How to requery form opened by another user

this may seem like an odd question, but I am wanting to requery a form that is opened by another user. Basically I have two different main menus, that pertain to the role of the employee. On these main forms I display a menu with the counts of clients in each status. I then have a main form that holds the clients data. After I update the status field I am requery both forms. Now this requerys the menu that is open on the current user that changed the status, but it doesn't requery the other menu that is opened by the other user unless they close the menu and reopen. Is this possible to requery another subform of another user's form.
Here is my code below:
Private Sub status_ID_AfterUpdate()
On Error GoTo Problems
DoCmd.RunCommand acCmdSaveRecord
Forms!frmNursesMenu!frmStatusCount_Nurses.Form.Requery
Forms!frmNursesMenu!frmStatusCount_Nurses.Form.Repaint
Forms!frmNursesMenu.Requery
Forms!frmNursesMenu.Repaint
Forms!frmAdminMenu!frmStatusCount.Form.Requery
Forms!frmAdminMenu!frmStatusCount.Form.Repaint
Forms!frmAdminMenu.Requery
Forms!frmAdminMenu.Repaint
Exit Sub
Problems:
Err.Clear
Resume Next
End Sub
You will need to use the form's timer event to requery the form every so often or look to see if the form needs a requery.
If your tables include a column with the time of the last edit then you could use that to see if the form needs a requery.

can docmd.gotorecord,,acnew open in new window?

I have a form which is bound to a table, and lets a user scroll through existing records in the table and make changes. I'm now trying to build a button which allows the user to insert a new record. So far I have a button with some basic vba:
Private Sub btnNew_Click()
DoCmd.GoToRecord , , acNewRec
End Sub
This opens a new record in the same form and has no way of assuring a user that the record has been saved. What i would really like is for the this to open a new 'pop up'form with a save button on
how would i do this?
Go to the design view of the Form, add a button name it saveBtn. Under its properties, set the visibility as No. Now in the Form Current method, check if the form has the new record, if so make the button visible.
Private Sub Form_Current()
Me.saveBtn.Visible = Me.NewRecord
End Sub
Using this method, the current form could be used for navigation and editing and when it sits on a new record a Save button will be available where in you could add the code to run a Save command or close the Form, which by default will save the record.

How to make a textbox not defualt to the first vaule in a database?

This may be something simple stupid, but its not working for me.
I made a table of rates in Access 2007.
I made a form for the table using create, split form, and followed the wizard.
I added a "Add Record", "Delete Record" and "Exit" button to the form.
For the auto created textbox, in properties, data, defualt value, I set it to "Rate"
When I first open the form the textbox always shows the first value in the database and will edit the first value in the database if I type in it. After I click the "Add Record" button it shows "Rate" and adds records to the end of the database.
So the question is, how do I get the textbox to read "Rate" and have it defualt to the end of the database when I first open it up I can add records?
Thank you,
This is relatively simple when using the switchboard. New switchboard item; Open Form in ADD Mode > Put in forms name. This would open the form at a new record.
Prgammically you can use docmd.openform eg: docmd.OpenForm(form_Name,acNormal,,,acFormAdd).
See Link in Remou's comment
HTH
Roger

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...