Open data bound form on specific record - vb.net

I am struggling to find an answer on this;
I have Form1 which contains a databound DataGridView table from a dataSet from a SQL server. Upon double click event of the row, I need to open Form2 to that databound specific ID. Now I understand that I can get each of the columns and loop through all the textboxes and add them in, but these seems very cumbersome and open to mistakes when there could be a large number of individual textboxes.
Instead I just want to open that form to the specific selected record. Something similar to how you could do with MS access...
DoCmd.OpenForm "Form2", , , "ID = " & recordID

Thank you very much for your help #Jimi. I used your solution by creating a new tableAdpter and adding a parameter which is passed to the new form, works perfectly.

Related

MS Access - Passing Data from a Subform to another Form

I have a main form (Form1) which is used to search records and the subform to that form contains all records searched. Editing or adding data is not available in Form1.
I want to be able to click on a record from the subform and click a button, to copy that particular record, to a seperate form (Form2). I want to do this so that the user doesn't have to manually copy and paste the data from Form1-subform to Form2.
I have tried continuous form but the maximum width of it is not enough. I'm new to Access and trying to figure out a way to solve this problem.
Can anyone suggest a way of doing this.
TIA
There are several ways to do this, i don't know the correct technical terms but i hope it can help;
Option 1 : Insert from subform to the relevant table that's conected to the main form
Example :
Form Data : tblCustomer
Subform Data : tblPotentialCustomer
So to add the data from the subform to the form just try something like this :
public sub Insert
Dim strSQL as string
Dim strPotentialCustomerName as string
Dim strPotentialCustomerKey as string
strPotentialCustomerName = me.subform.PotentialCustomerName
strPotentialCustomerKey = me.subform.PotentialCustomerKey
strSQL = "Insert into tblCustomer (CustomerName,PotentialCustomerKey) Values ('"& strPotentialCustomerName &"','"& strPotentialCustomerKey &"')"
call Currentdb.execute(strSQL,dbseechanges)
'--- BEWARE : In the mainform the new record is not automaticaly show, so you need an event to handle this.
end sub
Option 2 : Insert it to the main form directly using parent
Try to reach the relevant object like:
From your subform:
me.parent!CustomerName = mySubform.PotentialCustomerName
Hope this sets you in the right direction.
All the best !

Open a form to a record selected in a subform

I want to open a form to the record selected in the subform of a different form. I've tried following what I’ve seen in other posts about this, but I still can't get it to work. I think I'm just missing some very basic detail but this is my first Access database and I can't figure out what it is. Thanks in advance for your assistance.
Details:
F_Detail - (This is a Single Form containing details on a project.)
F_List - (This is a Single Form containing a subform and a button.)
subF_List - (This is the subform contained in F_List. This subform is in Datasheet view)
Project_ID - (This is the primary key contained in subF_List and in F_Detail. It is the common criteria between the two. It is Short Text type.)
subF_List displays row after row of projects. F_Detail displays details about a single project at a time. From F_List I want to select a row in subF_List and then click the button to open F_Detail, and F_Detail will be displaying details of the project whose row was selected in subF_List when I pressed the button.
What I Have for the Button:
On Click > Event Procedure
Private Sub ProjectDetailButton_Click()
DoCmd.OpenForm "F_Detail",,,"Project_ID = " & Me.Project_ID
End Sub
Upon clicking the button, I get an error saying, "Compile error: Method or data member not found" and it highlights the .Project_ID at the end of my code.
I don't know what I'm doing wrong and would appreciate any help you can lend. Please let me know if I've left out any needed detail.
Use apostrophe delimiters for text field parameters. Use # for date/time, nothing for number.
"Project_ID = '" & Me.Project_ID & "'"
Me is alias for form or report the code is behind. To reference a field in its RecordSource:
Me!Project_ID
Code on main form referencing field on subform must reference through subform container control. I always name container different from object it holds, such as ctrProjects:
Me.ctrProjects!Project_ID
I name controls different from fields they are bound to, such as tbxProject:
Me.ctrProjects.Form.tbxProject

MSAccess VBA: Pass values from a subform to a seperate form

I have a form frmDetail that contains several locked fields I want populated only when another form has been filled out, in the name of having some kind of standardization in my data. This other form frmSignOut is used to enter in a date and location that will populate the fields on frmDetail. frmSignOut also contains a subform subULookup that looks up users from a different table using an identifier number. The resulting last name, first name and phone # should also be passed to frmDetail. I also hope to combine first and last name somehow into a Last,First format.
My approach so far has been to open frmSignOut modally with acDialog and I inserted Visible=False into the On_Click event on frmSignOut. Then I try to reference my subform fields and set them to the desired fields on frmDetail. I finish by refreshing and then closing the dialog form.
Private Sub CmdSignOut_Click()
DoCmd.OpenForm("frmSignOut"),,,,,acDialog
If CurrentProject.AllForms("frmSignOut").isLoaded=True Then
Set Forms!frmSignOut!SubULookup.PhoneNbrTxt=Me.ContactNbrTxt
DoCmd.Close (acForm), ("frmSignOut")
Me.Refresh
End If
End Sub
I have only been able to get this far, trying to pull the first field PhoneNbrTxt. I can get frmSignOut to open, I put in data and when I click my 'close' command button I get run-time error 438: Object doesn't support this property or method.
The line highlighted is where I try to reference my subform field. I also have a 'Cancel' button added to frmSignOut that just closes the form, I have no errors there.
Again I'm completely new to Access without much prior experience in anything related, I'd appreciate any knowledge you guys can throw at me.
EDIT: I've been able to successfully pull the value to Me.ContactNbrTxt by adjusting my code to the below.
Me.ContactNbrTxt = Forms!FrmSignOut!SubULookup.Form!PhoneNbrTxt
It looks like the missing part was the Form! right before the control name, along with formatting this correctly and dropping Set.
Im still trying to work out how to combine first and last name before also pulling those to frmDetail, if anyone can help there.

Changes in DataTable not seen from another form

I have a DataTable in my DataSet which is not bound to any table in the SQL Database since I need it only inside 2 forms. When I update it in Form1, it works just fine, the table is populated and I can see the data inside a DataGridView (here is the code I use for updating it):
Dim newProcesRow As DataRow = Me.SampleDataSet.Tables("TableSample").NewRow()
newProcesRow("Col1") = val1.ToString()
newProcesRow("Col2") = val2.ToString()
newProcesRow("Col3") = val3.ToString()
Me.Sample_DataSet.Tables("TableSample").Rows.Add(newProcesRow)
After doing this, the DataGridView in Form1 shows the entire table with no problems. After the user clicks a button, Form2 is shown, where there is a DataGridView as well with the same DataSource as the grid in the first form, the unbound table from the DataSet. I also tryed putting a MessageBox in Form2 to return the number of rows from the DataTable and the result was 0.
Is there any way I could make Form2 see the rows in the DataTable without binding it to the database? (I don't want to do unnecessary changes to the DataBase...) Or are there any saves/changes/updates I am not doing to the DataTable?
If there is anything else I should mention in order to make it easier to find a solution, tell me and I will add. Thanks in advance for any piece of advice, have a nice day! (:

Use command button to open selected record on a form without filtering?

I have a continuous form which displays a small amount of data from each record in my table ProjectT (i.e. project name, status) and a command button in the footer which I would like to open the selected record in its expanded single form (where all of the relevant info is displayed).
At first I set this button up using Access's wizard, but realized that Access opens a selected record by filtering the data on the form. The problem with this is that once the expanded form is opened, I want a user to be able to move to other records without having to select to unfilter the results. If I change the button on my continuous form to simply open the expanded single form, is there code I can run to make the form open to the selected record without putting a filter on?
Initially I thought to set the expanded form's (named ProjectF) default value to Forms!ProjectListF!ProjectID (where ProjectListF is the continuous form and ProjectID is the autonumber primary key for ProjectT), but this was not successful, I think because there is more than one ProjectID displayed on ProjectListF.
Another thing to consider is that I have another button on my Main Menu form which opens the ProjectF form in data entry mode to prevent the user inadvertently changing/deleting an existing record when they are trying to add a new one; I have no idea if this might be important when trying to find a solution to my issue.
I'm open to any suggestion--I have an okay handle on SQL, and have delved into a little VBA but am completely self taught. Any ideas? Thanks!
You can open the detailed form with this command:
DoCmd.OpenForm "ProjectF", , , "[ProjectID] = " & Me!ProjectID.Value & ""