Import Excel data to Access database - vb.net

I want to import data from Excel to Access like this, it's working when my Access database has no password, but not working when it has a password.
I copy this code from internet.
Where do I have to put the password of Access?
Dim Access As String = "C:\FWS\Database1.accdb"
Dim Excel As String = "C:\New folder\2h.xlsx"
' Dim connect As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Excel & ";Extended Properties=Excel 8.0;"
Dim connect As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Excel + ";Extended Properties=""Excel 12.0 Xml;HRD=NO"""
Using conn As New OleDbConnection(connect)
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & "].[Password=frozbit].[tb_voucher2] SELECT * FROM [2hari$]"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
cmd.ExecuteNonQuery()
End Using
End Using

Change the cmd.commandText to be like this:
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & ";
PWD=frozbit].[tb_voucher2] SELECT * FROM [2hari$]"

From a quick search:
https://www.connectionstrings.com/access/
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;"
http://www.sqlstrings.com/ms-access-connection-strings.htm
"Driver= {MicrosoftAccessDriver(*.mdb)}; DBQ=C:\App1\Your_Database_Name.mdb;Uid=Your_Username; Pwd=Your_Password;"
Or
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\Your_Database_Name.mdb; User Id=admin; Password="
NOTE: These are untested.

please use your db password like my code.my password is 123.
Using cmd As New OleDbCommand()
cmd.Connection = conn
cmd.CommandText = "INSERT INTO [MS Access;Database=" & Access & ";PWD=**123**].[Sheet1] SELECT * FROM [Sheet1$]"
If conn.State = ConnectionState.Open Then
conn.Close()
End If
conn.Open()
cmd.ExecuteNonQuery()
MessageBox.Show("succussfully updated")
Form4.Show()
Me.Hide()

Related

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 in INSERT INTO statement for register

Dim con As New OleDb.OleDbConnection
Dim str As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=..\VisitorPass.accdb"
con = New OleDbConnection(str)
Dim sql As String = "insert into Visitor(Name,Password)values ('" & txtName.Text & "','" & txtPassword.Text & "')"
Dim cmd As OleDbCommand
con.Open()
cmd = New OleDbCommand(sql, con)
cmd.ExecuteNonQuery()
MsgBox("Account is Registered")
Try enclosing the field password in brackets.
insert into Visitor(Name,[Password])values ..
This should do the trick when using MS Access.

Search between two dates in access database using sql

This is my code for search in access database 2010. My problem is that when I search between two datetimepicker the result is wrong in datagridview, I mean when I search from specific records between May and June it shows me records also from February.
Private Sub Search_Record()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
Dim bookdetials As New frmContactDetails
Try
'get connection string declared in the Module1.vb and assing it to conn variable
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT contact_id, first_name , birth_date, book_num, send_from, no_answer, no_answer_by, rec, book_answer_name, book_answer_num, send_date, send_to, project_name FROM tblAddressBook"
If CheckBox1.Checked = True Then
sSQL = sSQL & " where project_name like '%" & Me.TextBox2.Text & "%' " & _
" AND birth_date between '" & DateTimePicker1.Text & "' AND '" & DateTimePicker2.Text & "'"
End If
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
Me.dtgResult.DataSource = dt
Label4.Text = dt.Rows.Count
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
datepicker text should be converted to datetime format in sql
I had the same problem, the solution was too silly but it worked
use text instead of datetime in the db
make sure the datetimepicker enters "short format" data

how to connect to a secured ms access database via vb.net using oledb

I have a database database1.mdb created in ms access 10.
I opened it in exclusive mode and set the password to abcd12.
I am using vb.net to INSERT data & DELETE data from that database but somehow it is not able to connect. Any ideas why connection cant be made?
Dim FilePath As String = "C:\Users\johnny\database1.mdb"
dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & _
FilePath & ";Jet OLEDB:Database Password=abcd12;")
Insert statement below:
dbconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & FilePath & ";Jet OLEDB:Database Password=abcd12;")
Try
dbconn.Open()
strSQL = "INSERT INTO myTable ([col1],[col2],[col3],[col4],[col5]) VALUES (?,?,?,?,?)"
objcmd = New OleDbCommand(strSQL, dbconn)
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("#col1", "user12345"))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("#col2", "user2345"))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("#col3", "user3456"))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("#col4", "user4567"))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("#col5", "user5678"))
objcmd.ExecuteNonQuery()
dbconn.Close()
Catch myException As System.Exception
Windows.Forms.MessageBox.Show(myException.Message)
End Try

VB.net 2010 dataset updates but access database remains same

I have tried to insert or update from my vb.net form into MS-Access database.
The dataset updates but the access database wont. Below is my code.
Try
Dim addLocation As String = "Insert into Provider (StateCode, Provider)" _
& "values ('" & ComboBox1.Text & "', '" & TextBox2.Text & "')"
Dim sqlcommand As New OleDbCommand
conn.Open()
With sqlcommand
.CommandText = addLocation
.Connection = conn
.ExecuteNonQuery()
End With
MsgBox("One record added", MsgBoxStyle.Information)
refreshGrid()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Private Sub refreshGrid()
cnString = "PROVIDER = Microsoft.ace.oledb.12.0;data source =" & Application.StartupPath & "\HCHPClosedIn.accdb"
sqlQRY = "SELECT * FROM Provider"
conn = New OleDbConnection(cnString)
Try
conn.Open()
da = New OleDbDataAdapter(sqlQRY, conn)
Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
da.Fill(ds, "Customers")
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Customers"
End Try
End Sub
Its been a while but I think I recall Access is kinda picky with commit. Try this:
With sqlcommand
.CommandText = addLocation
.Connection = conn
.ExecuteNonQuery()
.transaction = trans
End With
Trans.Commit()