I'm having a real hard time trying to get this funtion to work correctly
Public Function PopulateListContol(ByVal ListControl As Object, ByVal ValMem As String, _
ByVal DisMem As String, ByVal Database As String, ByVal DataTable As String, ByRef ds As DataSet)
Try
'connection stuff
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM " & DataTable, _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Database)
da.Fill(ds)
ListControl.DataSource = ds.Tables(0)
ListControl.DisplayMember = DisMem
ListControl.ValueMember = ValMem
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
I'm calling the function during a form load event in a block of code like
this:
'declare the datasets
Dim dsClientCorp As New DataSet()
'etc....
'fill combo boxes
PopulateListContol(Me.cboClientCorp, "ClientCorpID", "CompanyName", PathtoDB & ProjectDB, "ClientCorp", dsClientCorp)
'etc...
So far so good, everything seems to work. All the combos populate with the CompanyName field from the correct tables. When I try to write to another table, the ValueMember isn't writing out correctly. The wrong number is being inserted everytime.
I have an access database that consists of 10 tables (Projects, ClientPvt, ClientCorp, Architect, Contractor, LandArch, ProjMan, QuantSurv, ServEng, StrucEng) All are address books except the Projects table which stores information related to jobs (this is a tool for managing job data in an Architectural practice).
�Setup new project� form has various text boxes and a range of combos. Some of the combos are populated with the tables in the database. The only two fields required from each of the address-book tables are the "CompanyName" and "ID" fields. I want to display the CompanyName in each Combo but to to save the value in the ID field to the Projects table. Easy right?
In the case of the Architect table I have a combo called cboArchitect. cboArchitect is being populated with ArchitectID and CompanyName fields from the Architect table. CompanyName is being displayed in the combo and ArchitectID is being assigned as ValueMember (I hope). ArchitectID is the primary key in the table. When the new project form is complete and saved the ValueMember (ArchitectID) is written to the Projects table into a field called ArchitectID (a number field - Long Integer).
The save code:
Dim daProjects As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & PathtoDB & ProjectDB
Dim strSQL As String
Try
' Define a query string
strSQL = "INSERT INTO Projects ([Number], [Name], <<MORE FIELDS IN HERE>>, [ArchitectID]) VALUES ('"
strSQL = strSQL & Me.txtJobNumber.Text & "', '" & Me.txtJobName.Text & "', <<MORE VALUES IN HERE>>,'" & Me.cboArchitect.SelectedValue & "')"
'Connection Stuff
Dim ocon As OleDbConnection = New OleDbConnection(daProjects)
ocon.Open()
'Command Object to Execute the SQL
Dim ocmd As OleDbCommand = New OleDbCommand(strSQL, ocon)
ocmd.ExecuteNonQuery()
'Cleanup
ocmd = Nothing
ocon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
PathtoDB & ProjectDB combine to form the full path and filename of the database.
Please help, I'm totally confused on this one.
Many thanks
Russ