HELP! How To VB Form2 Opens Showing Current Record From Form1

By wbwalsh ·
Form1 (not datagrid) works great (add, delete, edit, scroll, etc..). Form1 uses table tbl_Offenders. tbl_Offenders has field(key) called CARDS_ID. CARDS_ID is a numeric, autonumber field.

Form2 (not datagrid) works great also. Form2 uses table tbl_Vehicles. tbl_Vehicles has a field(also a key) called CARDS_ID. CARDS_ID is a numeric text field. Form2 opens on a button located on From1. When Form2 open and the "Add" button is pressed, a new record is created with a text field (txt_Cards_ID) populated with the current selected record on Form1.

So I have two forms using two tables each with a CARDS_ID field. What I need is that when Form2 opens by pressing button on Form1, Form2 should only display records where the CARDS_ID field in Form2 (tbl_Vehicles) match the CARDS_ID field in Form1 (tbl_Offenders).

I have heavy Access knowledge and could setup Access forms to do this task... however I have begun to use Visual Studio (VB) and need help on this one issue. I'm using the designer to setup/load the forms, not using sql code directly in form.

Thanks for any help!!!

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

I recommend use a class to handle...

by TSoft In reply to HELP! How To VB Form2 Ope ...

I recommend to you use a class to handle you second form For instance...

--Add a class to your vbproject and renamed as cls_Vehicle

--Add a form to you project and renamed as frmVehicle
--now add two text boxes, let's say txtCode and txtDes

--now add the following method to frmVehicle
sub configure(byval strCode as string,byval strDes as string,byval carId as long,byref curCon as adodb.connection)
m_carId=carId '//declare m_carId at declarations section on frmVehicle (private m_carId as integer)
set m_conn=curCon '//declare m_conn at declarations section on frmVehicle (private m_conn as new adodb.connection)

'//here you can put the code to populate a recordset and show the
'//related records from tbl_Vehicles according to m_carId on a list Box for example...

end sub

--now add some methods and properties to handle you form

--First add the following code in declaration section from your class file

private m_form as frmVehicle '//it will handle the form
private m_code as string '//to set car code on frmVehicle
private m_des as string '//to set description car on frmVehicle
private m_carId as long '//to save current Id Car
private m_con as adodb.connection '//to handle database operations

--Now you need add some properties like this
public property get carCode () as string
carCode =m_code
end property

public property let carCode(byval value as string)
end property

--do the same for the m_des,m_carId and m_com fields as the code above

--Now Add to your class the following method

sub showForm

Set m_form = New frmVehicle

Load m_form


m_form.Show 1

end sub

--Add a method to finalize you frmVehicle and free memory..
--something like this

sub shutdown()

if not m_form is nothing then
set m_form=nothing

end if
end sub

--You should declare this variable at form level (form1) and free when you close
--form1 to improve performance
private dlg as cls_Vehicle

--Now you can call you form2 (frmVehicle) from form1
--put the following code inside the event you want to call form2 (frmVehicle)
--usually is RowColChange event I mean for each row on your grid

if dlg is nothing then set dlg=new cls_vehicle

with dlg

.connection=myConn '//this sould be your current ADODB.connection object

.showForm '//this code will show your form as a dialog box

end with

--put the following code on unload event on form1

--and finally on your command button on form2 (frmvehicle)
--add the following code
sub cmd_Click()
dim strQry as string

strQry="INSERT INTO tblVehicle(carId,name,des)" & _
"VALUES (" & m_carid & ",'" & txtCOde "'," & txtdes ")"
m_conn.execute strqry

unload me

end sub

This approach allow to accomplish you request in a very elegant way...
and will be more easy maintain your application.

Hope this can help..!!

Good Look!

Related Discussions

Related Forums