Solved

How to find SQL 2008 database path and use in code.

Posted on 2010-08-16
15
1,392 Views
Last Modified: 2013-11-28
I have an Access frontend with a SQL Server 2008 backend.  The Server has a LiveSales database as well as TestSales database.
I would like to write code in Access that fills a label box on the main form with "TEST DATABASE" if the current database is anything other then "LiveSales".  I need help with the code/connection string that I need to use.

If Current database is "LiveSales" then
else
   lblTestDb = "TEST DATABASE"
End if
0
Comment
Question by:BobRosas
  • 8
  • 7
15 Comments
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33450593
You can perhaps dig this out of the Hidden system table: msysObjects

If (DLookup("database", "msysObjects", "Name='SomeLinkedTableName'") Like "*LiveSales*") Then
    lblTestDB.Caption="Live Sales"
Else
    MsgBox "Test Database"
End If

The presumes that "LiveSales" is somewhere in the "database" field for this linked table.

You can run a query like this to see all linked tables in the Msysobjects table.
You can use the info in the query to customize this solution to work in your database:
SELECT MsysObjects.Connect, MsysObjects.Database, MsysObjects.DateCreate, MsysObjects.DateUpdate, MsysObjects.Flags, MsysObjects.ForeignName, MsysObjects.Id, MsysObjects.Name, MsysObjects.Type
FROM MsysObjects
WHERE (((MsysObjects.Type)=6));


JeffCoachman
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33450601
Code correction:

If (DLookup("database", "msysObjects", "Name='SomeLinkedTableName'") Like "*LiveSales*") Then
    lblTestDB.Caption="Live Sales"
Else
    lblTestDB.Caption="Test Database"
End If

This code again presumes that lblTestDB is infact a true "label" Control

;-)

JeffCoachman
0
 

Author Comment

by:BobRosas
ID: 33455885
Thank you for your comment.  Do you know if there is a specific Reference (Under Tools, References) that I should have checked or something else I need in order for this code to work?  I tried the code you gave me as well as...
SELECT MSysObjects.Database AS LinkPath
From MSysObjects
WHERE (((MSysObjects.Type) = 6));
and it won't even compile.  I get the error "Expected: Case" on line 1 with MSysObjects highlighted.  .  And I get "Expected: end of statement" on line 3 with the semi colon at the end highlighted.  I have never used this code before so I'm sure I'm missing something I just don't know what.
Thanks again for your help!
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33458436
1. What I posted was not "Code', it was the SQL for a query.
So create a Blank new query, go into "SQL View" and paste the SQL in and then run it. (View-->Datasheet View)

2. What you posted above is not the SQL that I posted here: http:#a33450593
What do you get when you run the SQL as I posted it?

JeffCochman
0
 

Author Comment

by:BobRosas
ID: 33458581
Sorry for the misunderstanding.
When I pasted your code into a blank query it does run but it comes up blank.  There is no data at all.
Any thoughts?
Thanks again.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33460359
Then there are no linked tables in the DB.

My presumption was that if this DB had an SQL back end, the tables would be linked (Via ODBC), and as such, would show up in the MsysObjects table.

How are you connecting to the SQL tables?
0
 

Author Comment

by:BobRosas
ID: 33466002
You are correct.  The access version I'm using is Access 2007.  I am linked to SQL Server 2008.  I'm using an ODBC connection but it's SQL Server Native Client 10.0.  Would that be the problem?
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33467529
In Access, can you open the linked table manager and see the SQL tables?
0
 

Author Comment

by:BobRosas
ID: 33467721
Yes I can.  The linked tables have an arrow and the globe and those that are not linked just have a grid icon.
LinkedTalbes.JPG
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33467845
And are these Linked tables visible when you run this:

SELECT *
FROM MsysObjects

If so, then you must check the "Type" field.
For standard Linked tables, it is 6
0
 

Author Comment

by:BobRosas
ID: 33468116
When I run...
SELECT *
FROM MsysObjects
I get 797 records but none of them have a type 6 and the field "Databasae" is blank for all records.
It does give the name of the database in some of the connection strings.  I could try parsing that out but I'm not sure that's the most reliable way to go.
0
 
LVL 74

Accepted Solution

by:
Jeffrey Coachman earned 500 total points
ID: 33468268
Yes, that was my suggestion, If you could get some way of distinguishing beteewn the connections of Test and LiveData, you would be able to determine what you were connected to.

Give it a try and see what happens.

Jeff
0
 

Author Comment

by:BobRosas
ID: 33468337
Thanks!
I wasn't sure it was ok to do it that way but I will give it a shot.
Thank you for all your help.
You hung in there and you were very helpful so I am going to max out your points.
I will post another question if I need more help.
Thanks again!
0
 

Author Closing Comment

by:BobRosas
ID: 33468346
Thanks so much!
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 33468743
And thank you for an interesting and thought provoking question...

;-)

Jeff
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now