Help in programming
so then .... I will help if you permit first a few remarks to your coding.
Usually for each form you get a Class.
f.e. You create a form called "Customer" you will also get a "Public Class Customer". If you later rename the form, the Class name remains the origin name. So if you create in the windows designer a form, you should name the form correctly and also rename the class to the same name as the form. Otherwise you (or me and others) get very confused. In your project for example you have a form called Customer, header text is "customer Form" and the according class is "CustomerForm". Unfortunately you have also a form called CustomerForm and class of that is called "Form3". This makes code reading very diffcult. I name the forms as follows: frmCustomer oder frmCustomerEdit and the class has the same name.
Next is the naming of objects in forms. Do not use the default naming of windows designer, f.e. textbox1, textbox2 and so on. This is ok for very short test-programms containing in maximum two or three objects. Please use names like txtCustomerID, txtFirstName etc. I prefer the naming of a textbox with the prevalue "txt" (for textbox) and the corresponding fieldname in the Database. In this way I have a chance to remember the object naming and know which value from the database is displayed in that textbox. I'm shure your talked about at your school....
So then let's do some carefully corrections:
- Form CustomerForm and the according class is named "CustomerForm"
- Form Customer and the according class is named "Customer"
- Add a module call mdlInitialize.mdl. This will be used for declaring some very public variables where are available over all the classes
-Place the Statement "Public strCustomerIdView As String" to the mdlInitialize
- I let your textboxes named as they are
the Button VIEW in then Customer-Form gets the following:
Private Sub btnview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnview.Click
'First you have to save the CustomID in a public variable, so the CustomerForm
' knows which Customer it has to show
'therefore you should define a Public CustomerID: Public strCustomerIdView as string
strCustomerIdView = DataGrid1.Item(DataGrid1.CurrentRowIndex, 0)
'declare the CustomerForm
Dim frmCustomerView As New CustomerForm
'Show the customerForm
frmCustomerView.Show()
End Sub
where the CusomerForm Load-Events is as followed:
Private Sub CustomerForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objConn As New OleDb.OleDbConnection
MyConn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;Data Source=D:\temp\ice management\ice management\ranz.mdb")
objConn = MyConn
objConn.Open()
Dim strSQL As String = "Select * From lamesa1 where [CustomerID]= """ & strCustomerIdView & """"
Dim da1 As New OleDbDataAdapter(strSQL, objConn)
Dim dtCustom As New DataTable
da1.SelectCommand.CommandText = strSQL
'Fill Datatable
Try
da1.Fill(dtCustom)
Catch ex As Exception
MsgBox("ERROR: Could not fill datatable" & vbCrLf & ex.ToString)
End Try
Select Case dtCustom.Rows.Count
Case 0
'should not occure because a Customer is shown in the previous form
MsgBox("No Records for Customer found")
Case 1
Dim arow As DataRow = dtCustom.Rows(0)
TextBox1.Text = arow.Item("CustomerID")
TextBox2.Text = arow.Item("FirstName")
TextBox3.Text = arow.Item("LastName")
TextBox4.Text = arow.Item("Address")
TextBox5.Text = arow.Item("City")
TextBox6.Text = arow.Item("Phone")
TextBox7.Text = arow.Item("Email")
Case Else
'should not occure, becaus eonly one customerID should be in the database
'BUT: In your Database CustomerID is duplikated
MsgBox("Duplikates for CustomerID = " & strCustomerIdView)
End Select
End Sub
Anything eles ???
Regards
murer