Eliminate "dbo_" for view and tables names in Access 2003 ADP linking to SQLSRV 2008 R2? - sql

I'm having an issue where I've created an Access 2003 ADP and connected to a database on SQL Server 2008 R2. On my PC, the table and view names are the same as in the SQL db (e.g., Table1, View1, etc), and I linked the report forms I built to these tables using those names. However, when I granted a colleague permission to the SQL backend so she can open the ADP and run the reports I built, it fails because on her PC a "dbo_" prefix appears on each table and object name (e.g., dbo_Table1, dbo_View1, etc), which of course breaks the connection to the data source for her. I'm stuck with Access 2003 at the moment. Is there a way to control this either in Access or in the back end? I did change her schema in SQL to dbo as an attempt to fix this, no dice. Thoughts appreciated!

The team I work on uses a small VBA utility to remove all those "dbo_" prefixes before running code against newly linked tables. It works well for us. You can add it in your Access application in whatever way is most convenient for you. Hope it helps. Good luck with your project.
Public Sub Zap_dboLabelsOnLinks()
Dim dbDef As Database
Dim tblDef As TableDef
Set dbDef = CurrentDb
'loop through each table in the database
For Each tblDef In dbDef.TableDefs
If UCase(Left(tblDef.Name, 4)) = UCase("dbo_") Then
tblDef.Name = Right(tblDef.Name, (Len(tblDef.Name) - 4))
End If
Next tblDef
Exit Sub

Related

Connect Microsoft Access to phppgadmin to query in PostgreSQL

My goal is to import data into Microsoft Access to create a database which I can reference from an excel dashboard for analysis.
I can't find any information on how to connect access to allow me to query the database on phpPgAdmin.
Any advice, direction or solution is highly appreciated.
Please let me know if there more details are necessary.
MS Access is a multifaceted thing as many tend to conflate and confuse its frontend GUI .exe application and the distinct backend database (JET/ACE SQL engine which are Windows .dll files). Most of the time we refer to its MS Office app. Technically, MS Access is really the same type of product as phppgadmin: a GUI console to a database, only its default database is the aforementioned engine but can also integrate other ODBC/OLEDB-connected backends including Postgres, Oracle, MySQL, SQL Server, etc.
Through various means, you can integrate MS Access as a medium between PostgreSQL and Excel without any single migration (export/import) of data.
Linked Tables - Directly connect to Postgres tables using its ODBC Driver.
Pass-through queries - Create saved queries using Postgres dialect within MS Access.
ADO Connections (see Importing data programmatically and by using functions) - Bypass MS Access and have Excel connect directly to Postgres also using OLEDB provider or ODBC driver. Below is the programmatic version showing two connection string examples, but you can save connection objects via the Excel ribbon UI.
Dim strConnection
' REFERENCE Microsoft ActiveX Data Objects, #.# Library
Dim conn As ADODB.Connection, rst As ADODB.Recordset
' ODBC AND OLEDB CONNECTIONS (SELECT ONE)
strConnection = "Driver={PostgreSQL};Server=IPaddress;Port=5432;" _
& "Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
strConnection = "Provider=PostgreSQL OLE DB Provider;Data Source=myServerAddress;" _
& "location=myDataBase;User ID=myUsername;password=myPassword;"
conn.Open strConnection
rst.Open "SELECT * FROM myPGTable", conn
By the way, above is the VBA version to be run in an Excel macro but ADO is a COM object and hence can be integrated in COM-interfaced languages including PHP, Python, R, Java, etc.

SubmitChanges() updates database in bin folder

My code and the Linq to sql function SubmitChanges are working, but when using a local database a copy of the database in the bin folder is updated and not the primary database. So the changes aren't shown on a new query. If I re-connect the database but don't load it as local same problem - the primary database isn't updated, but now I can't figure out which one is (tks to this question).
What setting for a local db or how do I use a non-local database to show changes on a new query of the database?
Dim DATA As New lnqPolarisDataContext
Dim newBOOK As New BOOK()
newBOOK.ID = 14
newBOOK.LEG = 11
newBOOK.P_C = "C"
newBOOK.STRATEGY = "STRADDLE"
newBOOK.STRIKE = 999
newBOOK.CONTRACT = "XXX"
DATA.BOOKs.InsertOnSubmit(newBOOK)
DATA.SubmitChanges()
... new query doesn't show these changes
maybe this is the best method?
The real solution in my opinion would be to put your database on the server where it belongs - after all, SQL Server is a server-based solution, not a file-based "database".....
install SQL Server Express (and you've already done that anyway)
install SQL Server Management Studio Express
create your database in SSMS Express, give it a logical name (e.g. YourDatabase)
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=YourDatabase;Integrated Security=True
and everything else is exactly the same as before...
Also see Aaron Bertrand's excellent blog post Bad habits to kick: using AttachDbFileName for more background info.

Can you backup a local SQL Server database with a "BACKUP DATABASE" script?

The problem is...
I have a small windows form application running .NET 4.0 Framework and a local SQL Server 2012 database file to store the data (.MDF file). This database is not being used in relation with SQL Server and is being managed through the Visual Studio Server Explorer.
The task that I have is that I want to do backups on this database through the software itself or a service. I have looked around the internet and I have found that there is a script BACKUP DATABASE [DatabaseName]. I have tried running this against my local database and it says the database name is not recognized.
This is the code I have tried.
Private Sub btnBackup_Click(sender As Object, e As EventArgs) Handles btnBackup.Click
Dim FileName As String = Now.ToString("ddMMyyyy_HHmm") & ".bak"
sfdBackupDB.InitialDirectory = txtBackupFolder.Text
sfdBackupDB.FileName = FileName
If sfdBackupDB.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
If Not File.Exists(sfdBackupDB.FileName) Then
SQLExecute("BACKUP DATABASE [ExampleDB] TO DISK = '" & sfdBackupDB.FileName & "' WITH FORMAT")
End If
End If
End Sub
sfdBackupDB is a save file dialog
SQLExecute is my own method to run a simple script but is literally just an SQL command call.
The error that I get from this goes:
Database 'ExampleDB' does not exist. Make sure that the name is entered correctly. BACKUP DATABASE is terminating abnormally.
There should be nothing wrong with the SQL Server connection that I am using as all other queries through the application are working so it can see the database is connecting to. Just in case it is important, the connection string is below:
Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database\ExampleDB.mdf;Integrated Security=True
I have also found another method using SMO objects but I get pretty much the same outcome.
So my main questions are:
Is this type of backup possible with this type of database
Are there any better methods of doing it
Forgive my lack of knowledge but I have not had much experience with this type of local database as I have only really worked on SQL Server databases in SQL Server Management Studio.

Convert t-SQL query with Cursor to Microsoft Access

One of my managers created a Access database and is working on some data analysis - what if scenarios. Based on different conditions, he produces a report in Access.
He asked me to do some data manipulation, so I imported the database into SQL and wrote a routine with a cursor that'll do what he wants. I then export the results back into Access. Before I get any heat for using a cursor, this was supposed to be a one time only deal, so that was the fastest way for me to get it done.
As you'd expect, now he wants me to run it all the time and asked me to convert my routine to Access so he can just run it. Before you tell me to just use SQL, he's very set on Access and is often traveling and off line.
So, my question is: is there a "easy" way to convert a T-SQL query with a cursor into Access? It's been a long time since I worked with Access, but I suspect it'd have to be re-written in VBA. I'm thinking that maybe another solution would be to call the query from Access and run it in SQL, but I don't know if that can be done or if it'd work on my case because of him being off line (maybe install SQL express in his laptop?)
Any suggestions would be appreciated.
Thanks,
Alex
This is how I got around it:
1.Downloaded and install SQL server express in the user's machine.
2.uploaded Access database structure and data to the local SQL.
3.created the stored procedure that I wanted to run in the local SQL server.
4.back in Access, deleted all the tables and recreated them as linked tables to SQL
5.Create a form in Access with a big button that executes the stored procedure
`Private Sub Command0_Click()
Dim qdf As DAO.QueryDef
Set qdf = CurrentDb.CreateQueryDef("")
qdf.Connect = CurrentDb.TableDefs("ANY TABLE").Connect
qdf.SQL = "EXEC dbo.[stored procedure name]"
qdf.ReturnsRecords = False
qdf.Execute
Set qdf = Nothing`
The stored procedure truncates one re-populates one of the tables. So after executing it I can open up the table in Access and see the changes. My manager can continue to use Access and SQL server is used in the back-end. Happy Ending! :)

access to SQL copy

We have an Access DB which has a set of local tables and input forms etc. in which a user maintains their data.
We also have a SQL DB with the same tables which is used to displays the data in a web search form.
What is the best way to allow the user to udate his changes to the SQL db while keeping the working copy local so he can work offline and then push the files when he is happy with new version of the data?
My first thought was add the SQL tables as linked tables I could then truncate (access does like that) or delete the content in each table and then do an insert for each table.
Can I call a SP from access on the SQL to truncate the tables as I am have problem running deletes
I really do want to get it down to the user running a macro/sql call that is repeatable etc.
Thanks for your help
You should be able to use the ADODB.Command object to execute stored procedures.
EDIT:
This example is copied from Using ADO in VB and Access
Sub ADO_COMMAND_CONNECTION_TEST()
Dim cmd As New ADODB.Command
Dim rs As ADODB.Recordset
Dim strConn As String
cmd.ActiveConnection = " DRIVER={SQL Server};" & _
"Server=UKDUDE;DATABASE=pubs;UID=sa;PWD=;"
cmd.CommandText = "byroyalty"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters(1).Value = 25
Set rs = cmd.Execute
' Recordset now has authors with 25% royalty.....
End Sub
Don't ever use MS Access linked tables with MS SQL.
Not only are they slow, but Access can leave open client-side write cursors on the tables referenced. That's a really dumb way to create lots of deadlocks, but Access does it anyway.
Microsoft significantly improved this when they added Access Data Projects - in these the entire back end is replaced with SQL and Access just supplies the forms.
If you want user actions to write directly back then ADPs are by far the best method.
If you want to cache changes locally in your Access DB and then send them up to SQL you have a far more complex problem. You will need to be far more specific on exactly how you want synchronisation to happen - for instance if two users make offline changes who wins when they connect?
I don't understand why you just don't link directly to the SQL Server data and use it directly, rather than going to the trouble of maintaining a second copy of it. This is the standard Access way to do things -- why are you resisting the natural capabilities of the tool you're using?