04-23-2011, 06:20 PM
I keep trying to figure out something that seems to be easy. But I've tried getting help from other coding forums and can't get any. So is there anyone here with a nice knowledge on working with these two programs together?
(04-24-2011, 02:57 AM)Untouch Wrote: [ -> ]I have had lots of past experience using both together for many programs although I'm probaly a bit rusty now.Okay my friend, so let me try to explain what's my problem as clearly as possible:
I can try to help if you have questions.
(04-23-2011, 07:02 PM)ReactioNz Wrote: [ -> ]Yeah. I heard Kobe is a great coder. I would message him if I were you.
(04-24-2011, 10:45 AM)Psycho Mantis™ Wrote: [ -> ]Okay my friend, so let me try to explain what's my problem as clearly as possible:
Basically, what I need to do is to bind TWO different database tables to ONE textbox in VB. The program itself is meant to have like multiple buttons on 'Form 1' and a textbox on 'Form 2' in which the user is supposed to insert a value which will then go to an Access database. But here's the hard part: IF the user presses 'Button 1' on 'Form 1', the Value inserted on 'Form 2' will go to a field in the database whereas if the user presses 'Button 2' to open 'Form 2', then the value will go to somewhere else and so on.
Now, this can be done using "Case". But the problem is that once the values are loaded into the textbox, I can't get them to save to the database. This is because I binded the textbox to the database using OleDbDataAdapter and OleDbDataConnection...
So basically I need to know how can you bind a database to VB using code only AND get it to save to the database...
I'm sorry if it sounds too confusing. Please see if this helps understanding: http://img849.imageshack.us/i/83594779.png/
Imports System.Data.OleDb
Public Class Form1
Dim SQL As String 'holds the SQL string sent to the database
Dim dbCommand As OleDbCommand 'sends the SQL string
Dim dbConnection As OleDbConnection 'connects us to the database
Dim dbFilePath As String = "C:\Users\KoBE\Documents\dbTest.accdb" 'file where database is located
Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
'create new connection
dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
'the sql (instructions)
SQL = "INSERT INTO tblTest (fldTest) VALUES ('" & TextBox1.Text & "')"
'connect to the database
dbConnection.Open()
'create new command to send the sql through the connection we just initiated
dbCommand = New OleDbCommand(SQL, dbConnection)
'send the command
dbCommand.ExecuteNonQuery()
'close the connection
dbConnection.Close()
End Sub
Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
'sql command
SQL = "SELECT fldTest FROM tblTest"
'new connection
dbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbFilePath)
'new command to send the sql
dbCommand = New OleDbCommand(SQL, dbConnection)
'data will be read via this DataAdapter
Dim data As New OleDbDataAdapter(dbCommand)
'creates a new data table that will store the data in memory
Dim table As New DataTable("Test")
'open the connection
dbConnection.Open()
'fill the table with the data
data.Fill(table)
'for each row show the value in the field 'fldTest'
For Each row As DataRow In table.Rows
MessageBox.Show(row.Item("fldTest"))
Next
End Sub
End Class