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

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.

Related

How to Open new form and insert ID from first form, Access [duplicate]

*****EDITED
So I have a form titled "NewInvoice". This form edits a table "Invoice" which contains the following; Invoice Number, Customer, Order Date. In this form I have a button which opens a subform titled "InvoiceItem". This edits a table with the same name that contains Invoice Number, Item Code, Dimensions, Etc.
Now currently, my button has an event procedure as follows.
Private Sub CreateInvoiceItem_Click()
DoCmd.OpenForm "InvoiceItem", OpenArgs:="InvoiceNumber"
End Sub
(The reason I am using a button and opening the form in a separate window is because I have cascading combo boxes in the sub form that become broken when I insert the sub form into the parent form)
Now where I am having trouble is setting the Form Load command. What I would like to occur is that the InvoiceNumber which is filled out in the Parent form auto fills in the sub form when the button is clicked.
Private Sub Form_Load()
**** NEEDED CODE****
End Sub
So try fixing the comboboxes as described in comment under question. Also, recommend code to requery the dependent combobox be in its GotFocus event. Keep in mind, cascading combobox with lookup alias will not work nice in continuous or datasheet form.
If you really want to pass value to independent form, the OpenArgs is a good approach.
Probably need to open the form to a new record row.
DoCmd.OpenForm "InvoiceItem", , , , acFormAdd, acDialog, Me!InvoiceNumber
Need code that makes sure the form is on a new record.
Private Sub Form_Load()
If Me.NewRecord Then Me!InvoiceNumber = Me.OpenArgs
End Sub
I find that the best way to do this is to add a Public sub to the form that you're opening and then pass whatever parameters you need to this function when you open the form. So to do what you're looking to do add a function like this to the form that you're opening;
Public Sub SetUpForm(InvoiceNumber as Long)
txtInvoiceNumber.Value = InvoiceNumber
End Sub
Where txtInvoiceNumber is the control on the form that you want to put the value into.
Then from your button;
DoCmd.OpenForm "InvoiceItem"
Forms!InvoiceItem.SetUpForm InvoiceNumber
This will pass your value for the invoice number to the control on the form that you're opening. This also gives you a lot more flexibility to control the process because you can pass more than one parameter to the sub and in the sub you can perform any number of tasks.

Refresh All Subform from Another Form

I have two main forms. One is the Dashboard and another is Edit.
After Editing, I have a button to reopen the Dashboard.
It can open the dashboard but I want it to refresh the subforms within that Dashboard.
This module will refresh the form you pass it, as well as all subforms. Requery will normally reset the selected item so before we requery we save the current record. If the record doesn't exist after requery it will gracefully go back to the top without error.
Public Sub RefreshForm(ByRef theForm As Form)
On Error GoTo ErrorHandling
Dim thisRecord As Long
thisRecord = theForm.CurrentRecord
Echo False
Dim childForm As Control
For Each childForm In theForm.Controls
If TypeOf childForm Is SubForm Then
childForm.Requery
End If
Next
With theForm
.Requery
.Recordset.Move thisRecord
End With
ErrorHandling:
Echo True
End Sub
To use it you simply drop this wherever you need a refresh:
RefreshForm Me
You shouldn't need to close and reopen anything. Any opened form and the controls on it can be requeried from VBA code running anywhere. It gets a little convoluted with subforms.
To requery a subform control on the current form: [SubformControlName].Requery
To requery the form in a subform control on the current form: [SubformControlName].Form.Requery
To requery a control in a subform on the current form: [SubformControlName].Form.Controls("ControlName").Requery
To do any of the above operations on a different form in the application, prefix with Forms![FormName].
Example: Forms![FormName].[SubformControlName].Form.Controls("ControlName").Requery

vb.net Move between two form with option

at first i am sorry for my weak english but i will try my best to explain my issue
i am new in programming so i got idea but i don't know if can be happen or not
the idea is , i have 2 form separated each form have there cods , but i want to add button in form one its open the second form, to add thing to the second one , but i need to return to first form when i close the second form
can i do something like that ??
for clarification the second form i can open it from other ways and have own function and the first form too
UPDATING !!!
at first thank for all your answers and not useful vote ,second yes i am new but not that much i know how to use formname.show() and formname.close() etc..... ,the issue not about usual ways to open and close forms, for example in form one i input information about user one of the option is where he live, the country choosing from dropbox ,next to the dropbox there button named add new country the button open new form to add new country when i add new country and closed the form back to the first form , and just to be know there button in the third form (setting) to open the form add new country ,IN SHORT WORD I DON'T NEED TO OPEN THE FIRST FORM EVERY TIME I CLOSE THE SECOND ONE , ONLY RETURN TO THE FIRST FORM IF I CAME FROM THE BUTTON THAT I CLICKED IN FORM ONE
put this on action
formname.show() 'for opening a new form
formname.close() 'for opening closing the form
formname.showdialog() 'Where you cant click the main form``
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'show form 2
Form2.Show()
'move text from one form to another
Form2.TextBox1.Text = TextBox1.Text
End Sub

How to launch a Look up form, and return the value from a combo box

Hopefully I can explain what I want to do well enough...here it goes...
I have a data entry form...the user will be entering employeeIDs. Once in normal operation, most people will be entering only their own EmpID, and they should know it, so this won't be a big problem 99% of the time once this DB goes live.
However, I need some temps to enter historical data from paper sheets into the DB. These people will not know anyone else's EmpID. I'd like to set the Student field's OnDblClick event in the subform's datasheet to open a small form with a combo box. The combo box has a list of all Employee Names, and is bound to the EmpID. Once this user enters the name, and selects the person, I have a button they can click to return to the datasheet.
I can use a function to launch the form, no problem there. But how do I return the EmpID to the field in the datasheet that was double clicked?
When user double clicks in the Student field...I want the next form to appear, and then once they type in the name and select the correct person...and then click Found Them!...I need that bound value to return.
I'd love to say I have code to share right now...but the only code I have is to launch the look up form. I'm brain farting on how to pull the value back down.
The way to do this to launch your little dialog form as “acDialog”. This will cause the calling code to WAIT.
And then the “magic” part is when they click on “Found Them” you do NOT close the popup form, but simply set the form’s visible = false. This has the effect of the calling code that popped up this form that halted to continue (the form is kicked out of dialog mode when you do this). So now your calling code continues.
So your code will look like this:
Dim strF As String ' name of popup form
strF = "frmPopUp"
' open form, wait for user selection
DoCmd.OpenForm strF, , , , , acDialog
' if for is NOT open, then assume user hit cancel buttion
' (you should likly have a cancel button on the form - that cancel buttion will
' execute a docmd.close
If CurrentProject.AllForms(strF).IsLoaded = True Then
' grab the value of thee combbo box
strComboBoxValue = Forms(strF)!NameOfComboBox
DoCmd.Close acForm, strF
End If
As noted, the code behind the Found Them button DOES NOT do a close form, but sets forms visible = false (me.Visible = false), and this trick allows the calling code to continue at which point you can examine any value on the form. Remember to then close the form after you grab the value.
It looks like your data table is in a subform so there is a little more work but it does not have to be as complex as the above solution if you don't want it to be. #Andre451 was close but you need the extra step of identifying the form and subform. For the purpose of demonstration let's call the form Attendance and subform Entry then I'll call the second form LookUp. So the code for your double click in the subform field will of course look something like this :
Private Sub Student_DblClick(Cancel As Integer)
DoCmd.OpenForm "LookUp"
End Sub
You really don't need anything else fancy there. For the button on "LookUp" you will put this:
Private Sub Command2_Click()
Forms![Attendance]![Entry].Form![Student] = Forms!Lookup!Student
DoCmd.Close acForm, "LookUp"
End Sub
And that should get you what you want without any overhead or having to leave any ghosts open.

MS Access does not add new Entry

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.