MS Excel connection with vb.net - vb.net

I have used the connection string below but I am getting an error when trying to create a table
Dim ConnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFName + _
";Extended Properties=""Excel 12.0 Xml;HDR=YES;IMEX=1"""
Cannot modify the design of table 'tablename'. It is in a read-only database.

If the database is read-only, then by definition you will not be able to create any tables in it.

Make absolutely certain that you do have write access to the file. For example are you accessing this from IIS which only has limited permissions. Check the security of the directory. Try a normal File.Open() of the file in the same process.

I'm personally using the following to connect to an access database:
_source = "..\db.mdb"
Dim strconnexion As String
strconnexion = "Provider=Microsoft.Jet.OLEDB.4.0;"
strconnexion &= "User ID=Admin;Password=;"
strconnexion &= "Data source=" & _source
_cnBd = New OleDbConnection (strconnexion)
_cnBd.Open()
Hope this helps.

Your problem is the IMEX=1. That tells excel to open in "import mode" making the connection read-only. I had the same issue, weird weird stuff.
Take that out and it works like a charm.

Related

Creating a Connection String with option - HDR=NO

note: I realize this question is similar to 512143, but this is in Excel only.
Although I could do this other ways, I wanted to learn the technique in This Article, which uses a recordset to transfer data. My problem is this: I don't have headers on my data (well, they are on the left... not above) so I was mysteriously NOT getting my first row into my template worksheet. I narrowed the issue down to the connection string NOT specifying the absence of headers.
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.FullName & ";Extended Properties=Excel 12.0;"
connDB.Open connStr
when I change it to:
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.FullName & ";Extended Properties=Excel 12.0;HDR=NO;"
I get the "Could not find installable ISAM" error, which I don't understand.
I searched for answers and found questions: 37251084, 512143
So I tried:
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.FullName & ";Extended Properties='Excel 12.0;HDR=NO;'"
which gave the error: "Format of initializing string does not conform to OLE DB specifications"
so now I will either give up (which is no good since I have to integrate this data system into Access eventually and should understand this type of thing.)
or do some makeshift fix where there are blank headers above the data. Which is lame.
After a few hours of searching for something I can understand relating to connection strings I am kinda spent, any help is appreciated.
This provider string worked on: Microsoft Excel 2016 MSO (16.0.9126.2109) 32-bit
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ActiveWorkbook.FullName &
";Extended Properties='Excel 12.0;HDR=NO;IMEX=1'"
for the full technique, I give credit to Felix Hooker at Datanumen. Here he gives the full code, which I found useful as a macro for creating saved data files from template in a scientific type data processing spreadsheet, without flashing windows open and closed when the user clicks a button!

submit data from excel userform to password protected access database

i am trying to submit userform data from excel (2013) to an access database.
without password, this code works fine.
Private Sub Addoer_Click()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim dbPath
dbPath = Sheet16.Range("K18").Value
Set cnn = New ADODB.Connection
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" & dbPath
Now i am trying to use same method to send data from excel to a password protected database (each user has different password). In the excel file, the user id is at Sheet16.Range("K17") and password is at Sheet16.Range("K19")
userid = Sheet16.Range("K17").Value
pw = Sheet16.Range("K19").Value
i changed the cnn.open line to following
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data source=" & dbPath, userid, pw, -1
and I am getting this error:
Error -2147217843 (Cannot start your application.The workgroup information file is missing or open exclusively by another User.)
i changed the cnn.open line to this
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" & dbPath, """ & userid & """, """ & pw & """, -1
and I get this error
Error -2147217843 (Not a valid account name or password).
Is there anybody that can help to point where did I do wrong?
Your second attempt doesn't work, because you are passing in the username as "user" when the username is actually user.
Regarding your first attempt, I found this note at connectionstrings.com:
Note! Reports say that a database encrypted using Access 2010 - 2013 default encryption scheme does not work with this connection string. In Access; try options and choose 2007 encryption method instead. That should make it work.
Also, could it be that the database is opened exclusively by another connection / program? If you have the database open in Access and you can design tables / queries, IIRC that means you have exclusive access to the database, and no other connections can connect to it. Do you see an .laccdb (or .ldb) file in the same folder as the Access database?
References:
Microsoft OLE DB Proivder for Microsoft Jet
ConnectionString Property
Formatting Rules for Connection Strings at connectionstrings.com

Excel VBA Database connection error: Cannot open database '(unknown)'

I am trying to connect to Access Database using ADO DSN.
StrPath = Sheets("Sheet1").Range("DB_location")
strCon = "DSN=MS Access Database;DBQ=" & StrPath & ";"
Set con = New ADODB.Connection
con.Open strCon
I can connect to DB without any problem on my machine. However, when I tried running this exact macro on another PC, I ran into this issue on the last line:
I assumed it was due to Database Engine, but installing "Microsoft Access Database Engine 2010 Redistributable" from Microsoft's website didn't solve this problem. All the necessary references in VBA editor are present.
There is no problem with DB or the macro, as I checked on a different PC, but this specific computer runs into this problem.
Changing
strCon = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " & StrPath & ";"
solved this problem, which means the problem was somehow due to DSN. Does anyone have a guess about where does this problem might come from?
I had this error and it ended up being my primary keys. WHen i fixed my primary keys in the table it worked like a charm.

SSCE - Multiple-Step OLE DB operation generated errors

I'm using a SQL Server Compact database (3.5). I have run through every article on this site (and others) I could find about this issue, but can't seem to find the problem. I am using Visual Studio 2012, VB.Net.
My provider string is:
Dim sqlConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.SQLServer.CE.OLEDB.3.5;Data Source=" & My.Application.Info.DirectoryPath & "\pw.sdf" & ";Password=XXXyyy1234;Persist Security Info=True;")
My code where it all breaks down is:
'Create SQL Query
Dim SQLString As String = "SELECT * FROM EmailGroups"
'Create Dataset, Open Connection
Dim dsPWC As New DataSet()
Dim OleDbDataAdapter1 As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter(SQLString, sqlConn)
sqlConn.Open()
And, of course, it breaks down on .Open().
I picked selecting from that datatable because it is a simple one.
EmailGroup_UID [int][PK]
EmailGroup_Name [nvarchar][length=50]
I'm sure I've been simply staring at this too long and can't see the forest for the trees anymore, so I'm hoping someone can slap me straight :)
Thanks in advance.
EDIT: This code all works when tying to the original MS Access 2007 database (accdb). Different provider, of course. And - I didn't import the tables, I re-created manually.
2nd EDIT: Immediate Window says 'A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll'
You should use SqlCeConnection instead of OldDbConnection.
if you must use OleDbConnection, use "ssce:database password" like so:
"Provider=Microsoft.SQLServer.CE.OLEDB.3.5;Data Source=" & My.Application.Info.DirectoryPath & "\pw.sdf" & ";SSCE:Database Password=XXXyyy1234;Persist Security Info=True;"

Can't connect to database, error 80040e21. Impossible to debug

I have a simple file that tries to connect to a database-
<%
Set RSDiscounts = Server.CreateObject("ADODB.Recordset")
RSDiscounts.ActiveConnection = "Data Source=serverName;Initial Catalog=dbName.dbo;Integrated Security=True"
%>
When I run it, I get-
error '80040e21'
/filename.asp, line 3
Searching for the error code doesn't help. My best guess is that something is specified in the connection string that shouldn't be there. But I used Visual Studio to create the string, and that connects to the database fine.
Is there any way I can figure out what's wrong? This seems like it's impossible to debug.
I think the problem is with your connection string - the string you have there is for ADO.Net but I don't believe that will work with ADODB.
Try a connection string like this:
Driver={SQL Native Client};Server=myServerName\theInstanceName;Database=myDataBase; Trusted_Connection=Yes
Or this is a connection string from one of my old projects with ADODB (from asp classic)
Provider=SQLOLEDB.1;Initial Catalog=databaseName;Data Source=serverName;Trusted Connection=Yes
That may not be 100% right, but you can find more details of all the connection strings you could want at the excellent ConnectionStrings.com.
From the library I wrote :
function SqlServerConnectionString( byval psDataSource, byval psCatalog, byval psUid, byval psPw)
'______________________________________________________________________________
'
' 'Sql Server Connection String'
'______________________________________________________________________________
dim x
x = "Provider=MSDASQL.1;Persist Security Info=False;User ID=" & psUid & ";Data Source=" & psDataSource & ";Initial Catalog=" & psCatalog
if psPw <> "" then
x = x & ";pwd=" & psPw
end if
SqlServerConnectionString = x
end function
I have similar routines for Firebird, Odbc and Access.
The problem will be you are trying to connect to the database using the user which is running the script. If this is running in IIS it will be something like USR_.
2 alternatives.
Give this user access to the database (I wouldn't do this one).
Create a SQL user and connect via this.
PROVIDER=SQLOLEDB;Data Source=serverName;Initial Catalog=dbName.dbo;USER ID=WebUser;PASSWORD=WebUserPassword;