Getting "data type mismatch in criteria expression" when I am trying to save date in excel vb form - vb.net
I have a user form from which I want to save data into excel, I can save a normal string and integer but when I am trying to save data from Label13(contains date selected from calendar) in below code, it gives me error as "data type mismatch in criteria expression"
Try
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim dataSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
Dim path As String = "D:\Hotel\RoomAvailability.xls"
Dim Sqlq As String
Dim Booked_From As string = Label13.Text
Dim Room_Number As String = 202
MsgBox(Booked_From)
MsgBox(Room_Number)
Sqlq = "Update [Sheet1$] Set [Booked_From] = "
Sqlq = Sqlq & "'" & Booked_From & "'"
Sqlq = Sqlq & " where [Room_Number] ="
Sqlq = Sqlq & "'" & Room_Number & "'"
MsgBox(Sqlq)
MyConnection = New System.Data.OleDb.OleDbConnection( "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter(Sqlq, MyConnection)
dataSet = New System.Data.DataSet
MyCommand.Fill(dataSet)
DataGridView1.DataSource = dataSet.Tables(0)
MyConnection.Close()
Catch ex As Exception
MsgBox(ex.Message.ToString)
End Try
Also what should be the format of cell in Excel in which i want to store date from Label13 ?
Related
Create table from Excel file
I'm trying to create a temp table on my database from an Excel file uploaded by the user. I cannot understand where is the problem and why Visual Studio is throwing that exception. Code Private Sub Excel_Load(ByVal filePath As String) Dim myConn As SqlConnection Dim myCmd As SqlCommand Dim sqlCmd As String Dim filename As String = Path.GetFileNameWithoutExtension(filePath) 'Setting up Connection' myConn = New SqlConnection("Server=*****;Database=*****;User ID=*****;Password=*****;Integrated Security=SSPI;") myConn.Open() 'Create table' sqlCmd = "CREATE TABLE XlsTemp AS (SELECT * FROM EXCELLINK [" & filename & "$])" 'Execute Query' myCmd = myConn.CreateCommand() myCmd.CommandText = sqlCmd myCmd.ExecuteNonQuery() myConn.Close() End Sub Exception SqlException: The object name 'EXCELLINK' is not valid. Peu_UNRAE is my Excel file.
At the end I come up with this solution: Dim MyConnection As System.Data.OleDb.OleDbConnection Dim fileExcelType As String //Get the file extension in order to use the propper provider If IO.Path.GetExtension(fileExcel.ToUpper) = ".XLS" Then fileExcelType = "Excel 8.0" MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & fileExcel & "';Extended Properties=" & fileExcelType & ";") Else fileExcelType = "Excel 12.0" MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & fileExcel & "';Extended Properties=" & fileExcelType & ";") End If //Opening excel connection MyConnection.Open() Dim myTableName = MyConnection.GetSchema("Tables").Rows(0)("TABLE_NAME") Dim MyCommand As OleDbDataAdapter = New OleDbDataAdapter(String.Format("SELECT * FROM [{0}] ", myTableName), MyConnection) MyCommand.TableMappings.Add("Table", " ") MyCommand.Fill(dt) //Closing connection MyConnection.Close() Remarks: I used // for the comments because the standar ' was giving some problem here on StackOverflow
select multiple column from excel in VBA
How can I select multiple column in Excel file using Vba. because I have excel file contains 580 columns. I try using Query below, but its only can select 255 columns. Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & TxtPath.Text & "; Extended Properties=""Excel 12.0 Xml; HDR=Yes""") ExcelConnection.Open() Dim expr As String = "select * FROM [Sheet1$]" Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection) Dim objDR As OleDbDataReader Dim SQLconn As New SqlConnection() Dim ConnString As String = "data source=" & DBServer1 & "; initial catalog=" & DBDataBase1 & "; persist security info=true; user id=" & DBUSer1 & "; password=" & DBPass1 SQLconn.ConnectionString = ConnString SQLconn.Open() Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn) bulkCopy.DestinationTableName = "TMP_UPLOAD_ISP" Try objDR = objCmdSelect.ExecuteReader bulkCopy.WriteToServer(objDR) objDR.Close() SQLconn.Close() Catch ex As Exception MsgBox(ex.ToString) End Try I try by using select column header (220 Columns to select) ,its getting error until I reduce down to 100 columns. Dim ExcelConnection As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & TxtPath.Text & "; Extended Properties=""Excel 12.0 Xml; HDR=Yes""") ExcelConnection.Open() Dim expr As String = "select Plan_Year,Colom4,Colom6,Colom7,Colom21,Colom22,Colom24,Colom25,Colom27,Colom28,Colom33,Colom34,Colom38,Colom39,Colom41,Colom42,Colom47,Colom48,Colom49,Colom50,Colom51,Colom52,Colom66,Colom67,Colom69,Colom70,Colom72,Colom73,Colom78,Colom79,Colom83,Colom84,Colom86,Colom87,Colom92,Colom93,Colom94,Colom95,Colom96,Colom97,Colom111,Colom112,Colom114,Colom115,Colom117,Colom118,Colom123,Colom124,Colom128,Colom129,Colom131,Colom132,Colom137,Colom138,Colom139,Colom140,Colom141,Colom142,Colom156,Colom157,Colom159,Colom160,Colom162,Colom163,Colom168,Colom169,Colom173,Colom174,Colom176,Colom177,Colom182,Colom183,Colom184,Colom185,Colom186,Colom187,Colom201,Colom202,Colom204,Colom205,Colom207,Colom208,Colom213,Colom214,Colom218,Colom219,Colom221,Colom222,Colom227,Colom228,Colom229,Colom230,Colom231,Colom232,Colom246,Colom247,Colom249,Colom250,Colom252,Colom253,Colom258,Colom259,Colom263,Colom264,Colom266,Colom267,Colom272,Colom273,Colom274,Colom275,Colom276,Colom277,Colom303,Colom304,Colom306,Colom307,Colom309,Colom310,Colom315,Colom316,Colom320,Colom321,Colom323,Colom324,Colom329,Colom330,Colom331,Colom332,Colom333,Colom334,Colom348,Colom349,Colom351,Colom352,Colom354,Colom355,Colom360,Colom361,Colom365,Colom366,Colom368,Colom369,Colom374,Colom375,Colom376,Colom377,Colom378,Colom379,Colom393,Colom394,Colom396,Colom397,Colom399,Colom400,Colom406,Colom407,Colom410,Colom411,Colom413,Colom414,Colom419,Colom420,Colom421,Colom422,Colom423,Colom424,Colom438,Colom439,Colom441,Colom442,Colom444,Colom445,Colom450,Colom451,Colom455,Colom456,Colom458,Colom459,Colom464,Colom465,Colom466,Colom467,Colom468,Colom469,Colom483,Colom484,Colom486,Colom487,Colom489,Colom490,Colom495,Colom496,Colom500,Colom501,Colom503,Colom504,Colom509,Colom510,Colom511,Colom512,Colom513,Colom514,Colom528,Colom529,Colom531,Colom532,Colom534,Colom535,Colom540,Colom541,Colom545,Colom546,Colom548,Colom549,Colom554,Colom555,Colom556,Colom557,Colom558,Colom559 FROM [Sheet1$]" Dim objCmdSelect As OleDbCommand = New OleDbCommand(expr, ExcelConnection) Dim objDR As OleDbDataReader Dim SQLconn As New SqlConnection() Dim ConnString As String = "data source=" & DBServer1 & "; initial catalog=" & DBDataBase1 & "; persist security info=true; user id=" & DBUSer1 & "; password=" & DBPass1 SQLconn.ConnectionString = ConnString SQLconn.Open() Using bulkCopy As SqlBulkCopy = New SqlBulkCopy(SQLconn) bulkCopy.DestinationTableName = "TMP_UPLOAD_ISP" Try objDR = objCmdSelect.ExecuteReader bulkCopy.WriteToServer(objDR) objDR.Close() SQLconn.Close() Catch ex As Exception MsgBox(ex.ToString) End Try How can I Select column that I want to select (220 columns intermittently).
You are using the ACE provider and that means you are bound by the limitations of MS Access. Access has a maximum field limit of 255 (see: Access 2016 specifications). Depending on the source worksheet, you could use a range query to retrieve multiple result sets and then merge them. Select * From [SheetName$A1:IU] then Select * From [SheetName$UI1:SQ] Column "UI" is column number 255 and column "SQ" is column number 511. If you are not familiar with this range syntax, omitting the row on the end column tells it to seek from the starting row until no data is found in the column range.
Syntax error (comma) in query expression, header with commas
I am trying to check if value from label1 exist in dbf file column named : "NALOG,C,8". Header in DBF file I can not change, 'cause it represents column's format and field size. But with that I get this error : "Syntax error (comma) in query expression "NALOG,C,8 = #NAL" Here is complete code : Dim con As New OleDbConnection Dim cmd As New OleDbCommand Dim FilePath As String = "C:\" Dim DBF_File As String = "PROMGL" Dim ColName As String = "NALOG,C,8" 'Dim SQLstr As String = "SELECT * FROM " & DBF_File con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _ " ;Extended Properties=dBASE IV;User ID=Admin;Password=" 'cmd = New OleDbCommand("SELECT * FROM " & DBF_File) cmd = New OleDbCommand("SELECT * FROM PROMGL WHERE " & ColName & " = #NAL") cmd.Connection = con con.Open() cmd.Parameters.AddWithValue("#NAL", Label1.Text) Using reader As OleDbDataReader = cmd.ExecuteReader() If reader.HasRows Then con.Close() Label6.Text = "EXIST" & TextBox1.Text TextBox1.Text = "" TextBox1.Focus() Else Label6.Text = "DOESN'T EXIST" End If end using Thanks.
If you have a column that is named this: Dim ColName As String = "NALOG,C,8" Then I would change it too this: Dim ColName As String = "[NALOG,C,8]"
Use this instead: Dim ColName As String = "NALOG"
Check if value exist in DBF database
I am trying to check if value "IAV-1419" exist in second column (ColName) in database named PROMGL.DBF. I get this error : No value give for one or more required parameters Dim con As New OleDbConnection Dim cmd As New OleDbCommand Dim FilePath As String = "C:\" Dim DBF_File As String = "PROMGL" Dim ColName As String = "[NALOG,C,8]" con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _ " ;Extended Properties=dBASE IV" cmd = New OleDbCommand("SELECT * FROM PROMGL WHERE [NALOG,C,8] = #NAL") cmd.Connection = con con.Open() cmd.Parameters.AddWithValue("#NAL", "IAV-1419") Using reader As OleDbDataReader = cmd.ExecuteReader() If reader.HasRows Then con.Close() Label6.Text = "EXIST" TextBox1.Text = "" TextBox1.Focus() Else Label6.Text = "DOESN'T EXIST" End If End Using I am stuck here, if anyone could please check this code for me.
are you sure that your connectionstring is correct???? Data Source=" & FilePath & how connection string know the database where it point only to "C:\" i think is missing database name Another personal suggestion make your code more simple to read in example : Dim FilePath As String = "C:\" Dim DBF_File As String = "PROMGL" Dim ColName As String = "[NALOG,C,8]" Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _ " ;Extended Properties=dBASE IV") con.Open() Using cmd As New OleDbCommand("SELECT * FROM PROMGL WHERE [NALOG,C,8] = #NAL", con cmd.Parameters.AddWithValue("#NAL", "IAV-1419") Using reader As OleDbDataReader = cmd.ExecuteReader() If reader.HasRows Then Label6.Text = "EXIST" TextBox1.Text = "" TextBox1.Focus() Else Label6.Text = "DOESN'T EXIST" End If End Using End Using End Using
Type Mismatch when combining two csv files in VB
I had my code working just fine, but when I generated new updated versions of the CSV files it suddenly errors out on me and gives me a type mismatch catch. Here is the code that I have right now. Dim A As String = "ADusers.csv" Dim B As String = "MlnExp.csv" Dim filePath As String = "C:\CSV" Try Dim ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & (filePath) & "\;Extended Properties='text;HDR=Yes'" Dim sSql As String = "SELECT *" _ & "FROM ([" & (B) & "] B LEFT JOIN [" & (A) & "] A ON B.EmployeeNumber = A.employeeID)" Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConnectionString) Dim Command As OleDb.OleDbCommand = New OleDb.OleDbCommand(sSql, conn) Command.CommandType = CommandType.Text conn.Open() Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sSql, conn) Dim dt As DataTable = New DataTable da.Fill(dt) DataGrid1.DataSource = dt DataGrid1.Update() conn.Close() lblStatus.Text = "CSV combined... Now saving to file." Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Exclamation) End Try Before it would go through, combine the two CSV files, and then display them in my datagrid. But now my catch is throwing back Type mismatch in expression
i would check B.EmployeeNumber = A.employeeID in both of your file one is a text value (left align) and the other is a a interger(right align)