M
Macca
Hi all,
This should probably go into the .net newsgroup as well.
I am using an Access 97 database (I have no control over this - it is part
of a package written for my company and is part of a web, customer and
administration package)
I am writing a tool to access this database and modify some of the contents
using oledb. I am going the path I have as it will often be updated via a
28k link.
I can cut and paste the following query into the access SQL window and
execute it, without a problem.
When I run the query in code from VN.net, I get an error (error in Insert
into) I have also attached the VB code.
Any suggestions please reply to the newsgroup (I have not supplied a
personal email for spam reasons)
Thanks for reading...
Query
INSERT INTO [User] (Username, Password, First, Surname, Phone, Fax,
PersonalEmailAddress, EmailDropBoxAddress, [Group], Position, BusinessUnit,
[Date], ReviewDate, Manager, ByWho) Values ('joe.bloggs', 'passwd', 'Joe',
'Bloggs', '124', '345', 'email@address', 'drop@address', 'small', 'member',
'one', '16/04/2003', '16/10/2003', 'Barney Rubble', 'Fred Nurks');
VB Code
Dim ODTcommand As New OleDb.OleDbCommand(strQuery, ODTConnection)
'open the connection
ODTConnection.Open()
'make a datareader
Dim ODTdatareader As OleDb.OleDbDataReader
'datareader command
ODTdatareader = ODTcommand.ExecuteReader
'get the data
If ODTdatareader.HasRows() Then
MessageBox.Show("The selected User Name already exists. Please
select another", "Duplicate User", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
ODTConnection.Close()
ODTcommand.Dispose()
Exit Sub
End If
ODTConnection.Close()
ODTcommand.Dispose()
'build the query string
Dim strsep As String = ", "
strQuery = "INSERT INTO [User] " & _
"(Username, Password, First, Surname, Phone, Fax,
PersonalEmailAddress, " & _
"EmailDropBoxAddress, [Group], Position, BusinessUnit, [Date], "
& _
"ReviewDate, Manager, ByWho) " & _
"Values ('" & cboUsername.Text & "', '" & txtPassword.Text & "',
'" & _
txtFirstName.Text & "', '" & txtSurname.Text & "', '" & _
txtPhone.Text & "', '" & txtFax.Text & "', '" & _
txtEmail.Text & "', '" & txtDropBox.Text & "', '" & _
txtGroup.Text & "', '" & txtPosition.Text & "', '" & _
cboBusinessUnit.Text & "', '" & txtStartdate.Text & "', '" & _
txtReviewdate.Text & "', '" & txtManager.Text & "', '" & _
txtbywho.Text & "');"
'strQuery = "User.username = """ & cboUsername.Text & """" & strsep
& _
' "User.Password = """ & txtPassword.Text & """" & strsep & _
' "User.First = """ & txtFirstName.Text & """" & strsep & _
' "User.Surname = """ & txtSurname.Text & """" & strsep & _
'"User.Phone = """ & txtPhone.Text & """" & strsep & _
'"User.Fax = """ & txtFax.Text & """" & strsep & _
'"User.PersonalEmailAddress = """ & txtEmail.Text & """" & strsep &
_
'"User.EmailDropBoxAddress = """ & txtDropBox.Text & """" & strsep &
_
'"User.Group = """ & txtGroup.Text & """" & strsep & _
'"User.Position = """ & txtPosition.Text & """" & strsep & _
'"User.BusinessUnit = """ & cboBusinessUnit.Text & """" & strsep & _
'"User.Date = """ & (txtStartdate.Text) & """" & strsep & _
'"User.ReviewDate = """ & (txtReviewdate.Text) & """" & strsep & _
'"User.Manager = """ & txtManager.Text & """" & strsep & _
'"User.ByWho = """ & txtbywho.Text & """"
MessageBox.Show(strQuery)
Debug.WriteLine(strQuery)
'make an oledb connection to the database (allows single fields to
be updated)
Dim ODTConnectionString As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\test.mdb")
ODTcommand.CommandText = strQuery
ODTcommand.Connection = ODTConnectionString
'open the connection
ODTCommand.Connection.Open()
ODTCommand.ExecuteNonQuery()
'close the connection
ODTCommand.Connection.Close()
This should probably go into the .net newsgroup as well.
I am using an Access 97 database (I have no control over this - it is part
of a package written for my company and is part of a web, customer and
administration package)
I am writing a tool to access this database and modify some of the contents
using oledb. I am going the path I have as it will often be updated via a
28k link.
I can cut and paste the following query into the access SQL window and
execute it, without a problem.
When I run the query in code from VN.net, I get an error (error in Insert
into) I have also attached the VB code.
Any suggestions please reply to the newsgroup (I have not supplied a
personal email for spam reasons)
Thanks for reading...
Query
INSERT INTO [User] (Username, Password, First, Surname, Phone, Fax,
PersonalEmailAddress, EmailDropBoxAddress, [Group], Position, BusinessUnit,
[Date], ReviewDate, Manager, ByWho) Values ('joe.bloggs', 'passwd', 'Joe',
'Bloggs', '124', '345', 'email@address', 'drop@address', 'small', 'member',
'one', '16/04/2003', '16/10/2003', 'Barney Rubble', 'Fred Nurks');
VB Code
Dim ODTcommand As New OleDb.OleDbCommand(strQuery, ODTConnection)
'open the connection
ODTConnection.Open()
'make a datareader
Dim ODTdatareader As OleDb.OleDbDataReader
'datareader command
ODTdatareader = ODTcommand.ExecuteReader
'get the data
If ODTdatareader.HasRows() Then
MessageBox.Show("The selected User Name already exists. Please
select another", "Duplicate User", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
ODTConnection.Close()
ODTcommand.Dispose()
Exit Sub
End If
ODTConnection.Close()
ODTcommand.Dispose()
'build the query string
Dim strsep As String = ", "
strQuery = "INSERT INTO [User] " & _
"(Username, Password, First, Surname, Phone, Fax,
PersonalEmailAddress, " & _
"EmailDropBoxAddress, [Group], Position, BusinessUnit, [Date], "
& _
"ReviewDate, Manager, ByWho) " & _
"Values ('" & cboUsername.Text & "', '" & txtPassword.Text & "',
'" & _
txtFirstName.Text & "', '" & txtSurname.Text & "', '" & _
txtPhone.Text & "', '" & txtFax.Text & "', '" & _
txtEmail.Text & "', '" & txtDropBox.Text & "', '" & _
txtGroup.Text & "', '" & txtPosition.Text & "', '" & _
cboBusinessUnit.Text & "', '" & txtStartdate.Text & "', '" & _
txtReviewdate.Text & "', '" & txtManager.Text & "', '" & _
txtbywho.Text & "');"
'strQuery = "User.username = """ & cboUsername.Text & """" & strsep
& _
' "User.Password = """ & txtPassword.Text & """" & strsep & _
' "User.First = """ & txtFirstName.Text & """" & strsep & _
' "User.Surname = """ & txtSurname.Text & """" & strsep & _
'"User.Phone = """ & txtPhone.Text & """" & strsep & _
'"User.Fax = """ & txtFax.Text & """" & strsep & _
'"User.PersonalEmailAddress = """ & txtEmail.Text & """" & strsep &
_
'"User.EmailDropBoxAddress = """ & txtDropBox.Text & """" & strsep &
_
'"User.Group = """ & txtGroup.Text & """" & strsep & _
'"User.Position = """ & txtPosition.Text & """" & strsep & _
'"User.BusinessUnit = """ & cboBusinessUnit.Text & """" & strsep & _
'"User.Date = """ & (txtStartdate.Text) & """" & strsep & _
'"User.ReviewDate = """ & (txtReviewdate.Text) & """" & strsep & _
'"User.Manager = """ & txtManager.Text & """" & strsep & _
'"User.ByWho = """ & txtbywho.Text & """"
MessageBox.Show(strQuery)
Debug.WriteLine(strQuery)
'make an oledb connection to the database (allows single fields to
be updated)
Dim ODTConnectionString As New OleDb.OleDbConnection( _
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\test.mdb")
ODTcommand.CommandText = strQuery
ODTcommand.Connection = ODTConnectionString
'open the connection
ODTCommand.Connection.Open()
ODTCommand.ExecuteNonQuery()
'close the connection
ODTCommand.Connection.Close()