Access VBA browseto pass parameters - ms-access

I have a tabbed navigation. I have a bound form a user double clicks on a single user out of a long list. I want to go to an unbound form, and pre-load the form with details about that user (edit the user). I'm specifically using an unbound form because I want to show a save/cancel button. I can't find any way to pass the id of the user into the load_form call. Am I missing something? Must I use some sort of global (this seems bad to me)?

You can use OpenArgs when opening the form:-
DoCmd.OpenForm "MyForm", acNormal,,,,,Args
And whatever Args is can be referenced in "MyForm" as Me.OpenArgs
e.g. if you pass a numeric PK then you could use something like:
dim db as dao.database
set db = currentdb
dim rs as dao.recordset
set rs = db.openrecordset ("select * from MyTable where PK=" & me.openargs", dbopendynaset, dbfailonerror)
if rs.eof then
'didn't find record with PK...
else
'then populate the unbound controls on your form with the fields from the recordset
...
Note that you don't need to have an unbound form just to offer a save/cancel. if you write an event handler for the form's Before Update event, you can cancel changes or commit them as you please.

Related

Passing keys from form to sub-form in Access

I'm using a sub-form to assign multiple tasks at the same time. This sub-form is placed on a form that has common details applicable to all the tasks there. For instance: Customer, Product Code, Part Code etc., Both the form and the sub-form feed the data to the same table. These forms are linked with one of the keys - Line Item, which is present on both the form and the sub-form. The other two keys are Task Title - placed on the sub-form, and Stage ID placed on the main form. Line Item is configured in a way that it populates the value from another open form for both the main form and the sub-form.
But Access isn't allowing me to add any details whatsoever to the sub-form. It gives me the error "You must enter a value for 'TableName.LineItem' field.
Kindly advise.
Two Approaches:
Select Table A and hit create form on the ribbon. Then do the usual prettification like deleting id columns, setting the forms format to continuous forms, and replacing textboxes with combo- boxes where appropriate.
First Approach is put unbound controls and a button in the header. Then insert the values from the controls in the button click event
Private Sub cmbAddRecord_Click()
Dim db As Database
Set db = CurrentDb
Dim rs As DAO.Recordset
Set rs = db.OpenRecordset("TableA")
rs.AddNew
rs!A = Me.txtA
rs!B = Me.txtB
rs!C = Me.txtC
rs.Update
Me.Requery 'show changes
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub
You can also insert values while actively entering the data for instance
Private Sub A_AfterUpdate() 'A is the TextBox in the detail section that is bound to A in TableA and so forth
Me.B = B.Parent!txtB
Me.C = C.Parent!txtC 'Tip use Me.MyControlName.SetFocus to select a control
Me.Refresh 'show changes
'tip you can also put this code in hotkeys https://learn.microsoft.com/en-us/office/vba/api/access.form.keydown
End Sub
'

Setting Form recordsource using DoCmd.Openform

I have a single form which can be accessed from multiple places in my Access 2013 app - depending on where the form is opened from, the recordsource should be different - for example if opening the form to search for a record, the form recordsource is a parameter query where the user enters the record id as the parameter - but if opening the form from elsewhere the recordsource is a select query and the record id is passed using VBA.
I want to avoid having twin forms if possible - I just want a single form and be able to set the recordsource when the form opens... is it possible to set the form recordsource upon opening the form (rather than after the form is opened)?
Have a bit of VBA that rewrites the query that your form reads from. Try this:
Set db = CurrentDb()
DoCmd.DeleteObject acQuery, "myFormQuery"
Set q = db.CreateQueryDef("myFormQuery")
q.Sql = "SELECT * FROM mytable WHERE 1=1;"
DoCmd.OpenForm "myForm", acNormal

How to move selected items from a listbox in ms access 2010 in another form in text boxes when click on listbox

How to move selected items from a listbox in ms access 2010 in another form in text boxes, one text box for each item from listbox, when click on listbox. Those are the forms, please help me.
form1
form2
You will need VBA in order to accomplish this.
On form1, you will need to check if form2 is open, if not, you can either have it stop the code and ask the user to open form2, or have the coding open the form automatically. I'm assuming you want it automatically...
Private sub List0_Click() 'Be sure to change List0 to the listbox control name you have given
Dim rst as DAO.Recordset
Dim strSQL as String
'Checks if current form is loaded, opens form if not
if not currentproject.allforms("form2").isloaded then
docmd.openform "Form2",acnormal
end if
'Creates query string to find specific record on listbox
strSQL = "SELECT * " & _
"FROM YourTableNameHere " & _
"WHERE (((YourTableName.YourFieldName) =" & me.List0.value & "));"
'Opens the recordset from the query string created above
Set rst = currentdb.openrecordset(strsql)
'Targets form2 and places values from recordset into form
With [Forms]![Form2]
![Your form2 control name] = rst![YourfieldNameForValue]
'Repeat for each field you wish to place a value in on your form2
End With
'Closes recordset and release object in memory
rst.close
Set rst = Nothing
'Validates the recordset is closed and object is released from memory
EndCode:
If not rst is nothing then
rst.close
set rst = nothing
end if
end sub
Again, be sure to change the control, table, and field names to match your current database design and naming convention.
Let me know if this works or not and I will make adjustments if needed.

How to create a temporary table and set it up as the record source for a subform?

I am trying to create a temporary table using the CreateTableDef method, and set that table as the record source of a subform in my access database when the parent form loads:
Private Sub Form_Load()
Dim db As Database
Dim tblDef As TableDef
Set db = CurrentDb
Set tblDef = db.CreateTableDef("tblMyTable")
tblDef.Fields.Append tblDef.CreateField("Field1", dbText)
tblDef.Fields.Append tblDef.CreateField("Fields", dbText)
tblDef.Fields.Append tblDef.CreateField("Field3", dbText)
db.TableDefs.Append tblDef
db.TableDefs.refresh
Me.sfrm.Form.RecordSource = "SELECT * FROM tblmyTable"
Me.sfrm.Form.Requery
End Sub
However, when the execution reaches to: Me.sfrm.Form.RecordSource = "SELECT * FROM tblIHC", it raises the run time error 2467: "the expression you entered refers to an object that is closed or does not exist."
Help is appreciated. Also, do I also need to set the source object property of the subform as well? And to what, if so.
It is much easier to simple use currentDB.Execute("CREATE TABLE tblMyTable (Field1 Text, field2 Text, field3 Text);"
But for the record source have you tried adding a ; at the end of the Select?
Do you have two subforms?
Should it read:
Me!IHCResults_subform.Form.RecordSource = "SELECT * FROM tblmyTable"
Me!IHCResults_subform.Form.Requery
And, if so, the last line is not needed. The subform will requery when changing the recordsource.
Also have in mind, that when you open the form, first the subform is opened (hidden), then closed, then the parent form is opened, and this opens the subform. So the subform must initially contain a valid recordsource.

Access 2003 FORMS: when I set at runtime with VBA the "RowSource" for a ListBox persist even if I close and then open

Access 2003 FORMS: when I set at runtime with VBA the "RowSource" for a ListBox persist even if I close and then open...
How to fix this, I would like to have clean "RowSource" when I open a new form...
You can set the RowSource during form load.
Private Sub Form_Load()
Dim strSql As String
strSql = "SELECT f.id, f.fname FROM foo AS f ORDER BY f.fname;"
Me.lstNames.RowSource = strSql
End Sub
Setting the RowSource of the List Box changes the Form's design. Access wants to save those changes for you (actually, I think the default behavior is to ask). If you want to close the form without changes, put this code in a Command Button's OnClick:
DoCmd.Close acForm, Me.Name, acSaveNo
The last parameter tells Access to not save the changes. Another alternative is what HansUp gave you in his second comment to his answer--just disable the List Box. Then when you figure out what its RowSource should be (on user input), set the RowSource & the Enabled property.