Connecting to Excel
I am trying to connect to Excel using OleDb, and insert a line of data through a form named "ProjFeed" that the user will input his data. Initially, I had a lot of errors but managed to fix them all. I then every time I run my codes I am not getting any error messages at all but also nothing happens; in other words the data are not transferred or entered into Excel. The following are my codes:
<Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click>
<Dim projconnect As OleDbConnection>
<Dim projCommand As New OleDbCommand>
<Dim projDataAdapter As New System.Data.OleDb.OleDbDataAdapter()>
<Dim projDataset As New System.Data.DataSet()>
<Dim projCurrencyManager As CurrencyManager>
<Dim sJobNo As String>
<Dim sYearMonth As String>
<Dim sSequence As String>
<Dim mail As New MailMessage>
<projconnect = New OleDbConnection("PROVIDER = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source= P:\Admin\Finance\2010-Financial Year\pharmout_project_number_register.xls;" & _
"Extended Properties=""Excel 8.0;HDR=YES""")>
<projconnect.Open()>
<projDataAdapter.SelectCommand = New OleDbCommand>
<projDataAdapter.SelectCommand.Connection = projconnect>
<projDataAdapter.SelectCommand.CommandText = "SELECT Number FROM [ProjectNumber$] WHERE (Mid(Number, 1, 2) = Format(NOW(), 'yy')) AND (Mid(Number, 3, 2) = Format(NOW(), 'MM'))">
<projDataAdapter.SelectCommand.CommandType = CommandType.Text>
<projDataAdapter.Fill(projDataset, "ProjectNumber$")>
<projCurrencyManager = CType(Me.BindingContext(projDataset), CurrencyManager)>
<sSequence = projCurrencyManager.Count + 1>
<sYearMonth = Format(Now(), "yy") & Format(Now(), "MM")>
<sJobNo = sYearMonth & Microsoft.VisualBasic.Right("00" & sSequence, 2)>
<projCommand.Connection = projconnect>
<projCommand.CommandText = "Insert into [ProjectNumber$]" & _
"(Number) VALUES('" & sJobNo & "')">
<projCommand.CommandText = "Insert into [ProjectNumber$]" & _
"(Country, City, [Company Name], Contact, [Project Task], [Hourly Rate], [Date Submitted], Comments)" & _
"VALUES('" & txtCountry.Text & "', '" & txtCity.Text & "', '" & txtClient.Text & "', '" & _
txtManager.Text & "', '" & txtDescription.Text & "', '" & txtRate.Text & "', '" & txtStart.Text & "', '" & txtComments.Text & "')">
<If txtCountry.Text = "" Or txtCity.Text = "" Or txtClient.Text = "" Or txtManager.Text = "" Or _
txtDescription.Text = "" Or txtRate.Text = "" Or txtStart.Text = "" Or txtComments.Text = "" Then
<MessageBox.Show("One or more field is/are blank/s:" & vbCrLf & "Please fill in all fields", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)>
<Exit Sub>
<Me.btnOK.DialogResult = DialogResult.Retry>
<End If>
<projCommand.ExecuteNonQuery()>
<projconnect.Close()>
I would appreciate it if you could help me with this problem.
Regards
Khalil
Last edited by Khalil; June 5th, 2010 at 01:21 AM..
|