Hi All
I have an application here that has 2 forms.
Form1 : Select data from database The database is configured on form load and the user selects data in a bound combo box, the valuemember property allows me to look up other column values and report them on form 1.
Fomr2: Edit database The form is triggered from form1 using a button. On load it connects to the database. Users can navigate through records (via next / previous buttons) and update or add new rows.
A lot of this is based on working examples from the home and learn web site.
I have tested both forms independently and they work fine. However when I try to use a common database for both I get the following response:
Form1 works fine.
Form 2 works as far as navigating records. But when I try to update or add new It fails at this line:
- it complains that the syntax is incorrect
As a test I created a second independent database and set form2 to point to it. Everything works fine and I can update and add new without issue.
It seems to me that the database connection I created in form1 is preventing form2 updating the common database - kind of a read only situation?
I am really struggling with this and not sure where to go now, I suspect my implementation of accessing the database is quite crude as I simply repeat the connection code from form1 in form 2, albeit with slightly different variable names.
Heres my code for setting up the connection in form1 (form 2 uses the same code in its load method but with different variable names):
Hers the code to add a new line in form2:
I would really appreciate any advice on this one......
Andy
I have an application here that has 2 forms.
Form1 : Select data from database The database is configured on form load and the user selects data in a bound combo box, the valuemember property allows me to look up other column values and report them on form 1.
Fomr2: Edit database The form is triggered from form1 using a button. On load it connects to the database. Users can navigate through records (via next / previous buttons) and update or add new rows.
A lot of this is based on working examples from the home and learn web site.
I have tested both forms independently and they work fine. However when I try to use a common database for both I get the following response:
Form1 works fine.
Form 2 works as far as navigating records. But when I try to update or add new It fails at this line:
Code:
da2.update(ds2, "Norbar2KE")As a test I created a second independent database and set form2 to point to it. Everything works fine and I can update and add new without issue.
It seems to me that the database connection I created in form1 is preventing form2 updating the common database - kind of a read only situation?
I am really struggling with this and not sure where to go now, I suspect my implementation of accessing the database is quite crude as I simply repeat the connection code from form1 in form 2, albeit with slightly different variable names.
Heres my code for setting up the connection in form1 (form 2 uses the same code in its load method but with different variable names):
Code:
Private Sub FrmComms_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CheckDataBaseLocation()
dbprovider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;" 'the technology we use to connect (Jet)
dbSource = "Data Source=" & Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\" & "Norbar2K.mdb"
con.ConnectionString = dbprovider & dbSource
con.Open()
Sql = "SELECT * FROM Models2k order by Description" 'Select all (* means all) columns from the tblContacts, order by "xxx" means assending order to "xxx"
da = New OleDb.OleDbDataAdapter(Sql, con) 'Create a Data Adator
da.Fill(ds, "Norbar2K") 'Using the data adaptor Fill The dataset with the address book
'MsgBox("Database is now open")
con.Close()
'MsgBox("Database is now Closed")
MaxRows = ds.Tables("Norbar2K").Rows.Count 'find number of rows in table, used to prevent trying to access past last row!
Inc = -1 'initial start for our row incremetion - ensures correct operation of buttons
'bind data set to combo like this:
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.ValueMember = "ID"
ComboBox1.DisplayMember = "Description"
End SubCode:
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Dim cb As New OleDb.OleDbCommandBuilder(da2)
Dim dsNewRow As DataRow
dsNewRow = ds2.Tables("Norbar2KE").NewRow()
dsNewRow.Item(1) = TxtDesc.Text
dsNewRow.Item(2) = TxtMaxTorque.Text
dsNewRow.Item(3) = TxtMinTorque.Text
dsNewRow.Item(4) = TxtAngle.Text
ds2.Tables("Norbar2KE").Rows.Add(dsNewRow)
da2.Update(ds2, "Norbar2KE")
MsgBox("New Record added to the Database")
BtnSave.Enabled = False
BtnNew.Enabled = True
BtnUpdate.Enabled = True >
BtnDelete.Enabled = True
End SubAndy