473,387 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to use VBA to find out if all of the tables are connected?

Seth Schrock
2,965 Expert 2GB
Is there a way to find out if all of the tables in the BE are connected? I would like to have a form open when the database is first opened. In its On_Load event, I want to check if all the tables are connected. If they are, then the form will close and the main form will open for the user to use the database. If the tables aren't connected, then I will use the startup form to enter the BE name and file path to be passed to my ReLink function (which is being worked on in another thread). I just need to know how to check if the tables are connected. I have gone through all the TableDef properties on MSDN, but the information is very scarce. I couldn't find anything that said "Check Connection", so I'm guessing that there isn't anything explicitly for this, but I'm also guessing that there is a way of finding out if it is connected by checking something like record count or something like that. I just don't know how things will be returned so I don't know how to check for them. I did find out that the record count for a linked table will always be -1. If the BE is disconnected, would it still return a -1 because it knows that it is a linked table or would it return something else?

Just tested my last guess and it still returned -1. Is there anything else like that that could be tested and would return a different value if not connected?
Dec 8 '12 #1

✓ answered by NeoPa

Interesting. I get the same issue, which means the code I suggested isn't reliable in all circumstances. Due to the delay in responding (Network time-out), I'm guessing that it is happy to report on the status of a file, but expects the reference to be to a valid and reachable device. As the share referred to is on a server that is not only unreachable, but also indeterminable (it can't even work out where to look for it), I'm guessing that's why it crashes rather than returns an empty string.

I would recommend handling this in the Exist() procedure. A very good reason why this procedure should be left as an autonomous unit. Mixing it up with the rest of your logic is certainly not recommended. It goes against all the rules of programming.
Expand|Select|Wrap|Line Numbers
  1. 'Exist() returns true if strFile exists.  By default ignores folders.
  2. '22/05/2003 Rewritten with better code.
  3. '20/05/2005 Added finding of R/O, System & Hidden files.
  4. '11/12/2012 Added handling of inaccessible drives.
  5. Public Function Exist(strFile As String, _
  6.                       Optional intAttrib As Integer = vbReadOnly Or _
  7.                                                       vbHidden Or _
  8.                                                       vbSystem) As Boolean
  9.     On Error Resume Next
  10.     Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
  11. End Function

23 5653
zmbd
5,501 Expert Mod 4TB
The only way I've found is to attempt to open a record set on the linked table, try a record move, and trap for error.
I do this on just one linked table in the backend and assume if it fails they all do and if it passes then they all pass; however, you could do this for each attached table in the tabledefs.
Dec 8 '12 #2
NeoPa
32,556 Expert Mod 16PB
I suspect any action on a table linked to air would result in a failure Seth.

It seems you have two options :
  1. Access the table somehow and trap the error.
  2. Identify the file name from the .Connect property (Your other recent thread has an example of how to do this,) and check for that file using Dir(). Some example code, if required, is :
    Expand|Select|Wrap|Line Numbers
    1. 'Exist() returns true if strFile exists.  By default ignores folders.
    2. '22/05/2003 Rewritten with better code.
    3. '20/05/2005 Added finding of R/O, System & Hidden files.
    4. Public Function Exist(strFile As String, _
    5.                       Optional intAttrib As Integer = vbReadOnly Or _
    6.                                                       vbHidden Or _
    7.                                                       vbSystem) As Boolean
    8.     Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
    9. End Function
Clearly, you would need to process through your TableDefs using a For Each Next loop.
Dec 8 '12 #3
Seth Schrock
2,965 Expert 2GB
So option 2 works based on the (logical) assumption that if the BE exists where the FE thinks it is, then there is a connection? That makes sense. And I would loop through the TableDefs because it is possible that there are multiple BE files (however unlikely that would be) so each table would pass its location to the function to check for true.

I thinking of the whole picture, I see two options. If a table Exist() comes back as false, then I could either immediately jump to where I would relink all the tables, or I could add the tables to a recordset of disconnected table. I would then loop through that recordset in the ReLink function. Which would you recommend?
Dec 8 '12 #4
zmbd
5,501 Expert Mod 4TB
Seth,
I would look at a "both/and" instead of an "either/or."

I've had two occasions where the DIR() will show that the file indeed exists; however, once the user tried to do anything in the backend we were sunk.

What happened is that a new Employee had the default "List Folder Contents" access rights to the directory; thus could see that file, but did not have the R/W/M.

To get around this situation, I check for the backend file using the DIR() using the stored location in the local table or which you can get as Neopa states and then proceed as in my earlier post.
Dec 8 '12 #5
Seth Schrock
2,965 Expert 2GB
If you are going to open the recordset and do the error trapping like you said, why would you try to find the file as NeoPa said? I understand why you don't like relying on the file being where it is supposed to be, but I don't understand why you would use Dir() if you are going to use the other method anyway.
Dec 9 '12 #6
NeoPa
32,556 Expert Mod 16PB
It is certainly safer and more thorough to avoid the assumption that just becasue a file exists it is necessarily available for access.

As ReLink() takes a BE file parameter though, rather than a TableDef, I would would use it for each BE linked to rather than all TableDefs you find.

Indeed, checking for the existence of a BE makes little sense to me if you are already aware that the link is not effective for one reason or another.
Dec 9 '12 #7
zmbd
5,501 Expert Mod 4TB
Short:
Because I deal with backends that have thousands of records, there's a sense of logic involved, and because I have an aversion to error trapping as a programing method to handle things that can be done so within the schema.


A little longer:
- If the file is either not there or not accessible to the user, then logically it makes no sense to attempt the record open. Instead, I inform the user that the file doesn't appear to be in the normal location and ask them if they want to take a stab at locating the file. This being the first opportunity to do so; thus, the best logical point to branch the code for a file search.

- If the file is there, then I check the links. I no-longer assume that the user will have read/write/modify rights. Nor, have I at this point needed to load any of the libraries etc... because we haven't needed to thus, smaller overhead in the pc. I do assume that if the small table is good, the big table will be good to... in the one case where I do not have a small single row table, the select record set I open is for a "TOP 1" if that fails then I assume the rest of the connections fail too. (and when that employee leaves the plant... there will be some changes made to that database - as in, it'll go away we already have that same information in the main datatables. sigh)

- Hold over from my professors. They taught that although error trapping was a good practice, it was not good practice to use it for anything that can be properly handled within the built-in structure of the engine. In this case, there is not other means of testing the actual connection within the MSA/MSO-VBA that I have found.
Dec 9 '12 #8
NeoPa
32,556 Expert Mod 16PB
I certainly wouldn't argue with that approach Z, however :
  1. There seems little benefit (to me) to add an element that doesn't use error trapping, if error trapping will be required at some point in the logic anyway. Access and VBA don't seem to share your POV, and there are logic holes in the system where the only viable way (I've found at least) to determine something is to smash it one and see if it breaks. Having lost that battle, there's very little point in introducing partial solutions that are purer in themselves, when they are simply part of a logic whole which has been corrupted by the error handling approach.
  2. Your approach does seem a more polished approach, but seems to diverge somewhat from the subject of the question. IE. A better answer, but for the reason behind the question rather than the question itself (I would say).

That said, as long as that is understood, what you say makes sense. Personally, I'd prefer to focus on understanding the fundamental issue before fixing it up with what probably constitutes a more thorough approach. I expect though, that now Seth has that context to what you said earlier, that he can fit it into his overall understanding quite neatly.
Dec 9 '12 #9
Seth Schrock
2,965 Expert 2GB
Finally getting to test my knowledge of what everyone has told me. I was able to test this at work when the tables were really connected but got called away before I could move the BE to another location to break the link. I am now working on it from home which automatically put the BE in another place and now I'm getting a run-time error #52 that says "Bad file name or number". Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Public Function Exist(Optional intAttrib As Integer = vbReadOnly Or _
  2.                                                       vbHidden Or _
  3.                                                       vbSystem) As Boolean
  4. Dim strFile As String
  5. Dim db As DAO.Database
  6. Dim tdf As DAO.TableDef
  7. Dim intParam As Integer
  8. Dim varLinkAry As Variant
  9.  
  10. Set db = CurrentDb()
  11.  
  12. For Each tdf In db.TableDefs
  13.     With tdf
  14.         If .RecordCount = -1 Then
  15.            varLinkAry = Split(.Connect, ";")
  16.            For intParam = LBound(varLinkAry) To UBound(varLinkAry)
  17.                 If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
  18.            Next intParam
  19.  
  20.            strFile = Mid(varLinkAry(intParam), 10)
  21.            Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
  22.            If Exist = False Then Exit Function
  23.         End If
  24.     End With
  25. Next tdf
  26. MsgBox ("All tables are connected")
  27. DoCmd.Close acForm, "frmSplash"
  28. DoCmd.OpenForm "frmTest"
  29.  
  30. End Function
  31.  
The error highlights line 21. I checked the value of strFile and it came up with
\\Ftcbank1\docs\sschrock\My Documents\Database Stuff\BackendTesting\Location1\BackendTesting_be.a ccdb which is correct for the location at work. What I don't get is why is this failing when the whole purpose of that line is to see if it exists?

I do plan on opening the recordset and do the error trapping, but since I have never done work on either part, I decided to tackle one step at a time and decided to start with the step that came first in the logic.
Dec 11 '12 #10
NeoPa
32,556 Expert Mod 16PB
Interesting. I get the same issue, which means the code I suggested isn't reliable in all circumstances. Due to the delay in responding (Network time-out), I'm guessing that it is happy to report on the status of a file, but expects the reference to be to a valid and reachable device. As the share referred to is on a server that is not only unreachable, but also indeterminable (it can't even work out where to look for it), I'm guessing that's why it crashes rather than returns an empty string.

I would recommend handling this in the Exist() procedure. A very good reason why this procedure should be left as an autonomous unit. Mixing it up with the rest of your logic is certainly not recommended. It goes against all the rules of programming.
Expand|Select|Wrap|Line Numbers
  1. 'Exist() returns true if strFile exists.  By default ignores folders.
  2. '22/05/2003 Rewritten with better code.
  3. '20/05/2005 Added finding of R/O, System & Hidden files.
  4. '11/12/2012 Added handling of inaccessible drives.
  5. Public Function Exist(strFile As String, _
  6.                       Optional intAttrib As Integer = vbReadOnly Or _
  7.                                                       vbHidden Or _
  8.                                                       vbSystem) As Boolean
  9.     On Error Resume Next
  10.     Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
  11. End Function
Dec 11 '12 #11
Seth Schrock
2,965 Expert 2GB
Unfortunately, I am very illiterate when it comes to rules of programming. I really wish that I didn't have to self teach myself everything as I feel that I have many gapping holes in my knowledge foundation :(

So your suggestion is to make line 21 of my code its own function? Should I put the other part of the code in the form's code or leave it as a to be called from the form which will in turn call Exist(strFile)?

I also have a question about how accurately I copied your code. Am I supposed to include lines 6, 7, and 8 or just choose one of the options?

In thinking of the whole picture, I see two options. If a table Exist() comes back as false, then I could either immediately jump to where I would relink all the tables, or I could add the tables to a recordset of disconnected tables. I would then loop through that recordset in the ReLink function. Which would you recommend?
Dec 11 '12 #12
NeoPa
32,556 Expert Mod 16PB
Seth:
Unfortunately, I am very illiterate when it comes to rules of programming. I really wish that I didn't have to self teach myself everything as I feel that I have many gapping holes in my knowledge foundation :(
Maybe you could browse through Allen Browne's site (Tips for Programmers). There's a lot there to get you going.
Seth:
So your suggestion is to make line 21 of my code its own function? Should I put the other part of the code in the form's code or leave it as a to be called from the form which will in turn call Exist(strFile)?
I would suggest calling the Exist() function at that point, but your whole code seems to be within an Exist() function. It really shouldn't be in one called that or even declared that way.
Seth:
I also have a question about how accurately I copied your code. Am I supposed to include lines 6, 7, and 8 or just choose one of the options?
You would copy the whole procedure exactly as is. Those lines ensure that the default value of intAttrib is set to recognise normal files as well as any files with any of those attributes set. It normally works fine as a default. One would rarely need to specify that value themselves.
Seth:
In thinking of the whole picture, I see two options. If a table Exist() comes back as false, then I could either immediately jump to where I would relink all the tables, or I could add the tables to a recordset of disconnected tables. I would then loop through that recordset in the ReLink function. Which would you recommend?
It really depends on the structure of your database, and was handled specifically in a separate thread (where this question is more relevant). The procedure found in the other thread was designed to relink all tables found in the database that currently linked to a BE A, to a BE B. In that scenario, it makes good sense to save a list, not of linked tables, but of BEs linked to, along with the replacements for each. As I say, it all depends on the structure of your database.
Dec 11 '12 #13
Seth Schrock
2,965 Expert 2GB
The last part of my question was more asking what to in the part of the code that checks if e file exists: stop on the first table that comes back as does not exist or just add it to a list of unlinked tables and continue checking the other tables? My current code stops as soon as it is discovered that the table's BE file doesn't exist. I just felt this question had more to do this this side than the rekindling side. If the way that I just explained it still falls under your original understanding, I will post it in my other question.

I will try splitting the exist function out like you have it and put the other code back in the form's On_Load event.
Dec 11 '12 #14
Seth Schrock
2,965 Expert 2GB
Okay, I have split the Exist() function to just test if the file exists and I get the same error message on the same code. So here is what I've got:

Expand|Select|Wrap|Line Numbers
  1. Public Function Exist(strFile As String, _
  2.                       Optional intAttrib As Integer = vbReadOnly Or _
  3.                                                       vbHidden Or _
  4.                                                       vbSystem) As Boolean
  5.  
  6.  
  7. Exist = (Dir(PathName:=strFile, Attributes:=intAttrib) <> "")
  8.  
  9.  
  10. End Function
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2. Dim strFile As String
  3. Dim db As DAO.Database
  4. Dim tdf As DAO.TableDef
  5. Dim intParam As Integer
  6. Dim varLinkAry As Variant
  7.  
  8. Set db = CurrentDb()
  9.  
  10. For Each tdf In db.TableDefs
  11.     With tdf
  12.         If .RecordCount = -1 Then
  13.             varLinkAry = Split(.Connect, ";")
  14.             For intParam = LBound(varLinkAry) To UBound(varLinkAry)
  15.                 If Left(varLinkAry(intParam), 9) = "DATABASE=" Then Exit For
  16.             Next intParam
  17.  
  18.             strFile = Mid(varLinkAry(intParam), 10)
  19.             If Exist(strFile) = False Then Exit Sub
  20.         End If
  21.     End With
  22. Next tdf
  23. MsgBox ("All tables are connected")
  24. DoCmd.Close acForm, "frmSplash"
  25. DoCmd.OpenForm "frmTest"
  26.  
  27. End Sub
  28.  
Dec 11 '12 #15
NeoPa
32,556 Expert Mod 16PB
Seth:
If the way that I just explained it still falls under your original understanding, I will post it in my other question.
It's already answered in your other question, as well as here if you read what I posted. The answer isn't a simple yes/no, so maybe you're not seeing it easily.
Seth:
Okay, I have split the Exist() function to just test if the file exists and I get the same error message on the same code. So here is what I've got:
The code you have there for the Exist() function is not the fixed code I (just) posted in #11. This code was updated to handle exactly the error you've fallen over, so will certainly fall over if you use this older version.
Dec 11 '12 #16
Seth Schrock
2,965 Expert 2GB
Just when I think I'm learning, I miss two answers in one post. Great way to learn.

Okay, as you probably already knew, that extra line (which I missed on first reading) made the whole thing work beautifully.

Now, on to the error trapping portion. Would I be correct that I should pass the table name from the code that I already have in the form's on_load event to a separate Connected() function and do the error trapping in that?
Dec 11 '12 #17
NeoPa
32,556 Expert Mod 16PB
Seth:
Just when I think I'm learning, I miss two answers in one post. Great way to learn.
There's really no need to be so hard on yourself Seth. Certainly, try to proceed carefully and attentively, but there's much here and it's quite easy to make mistakes. A gentle nudge in the right direction is all you need. You're already making visible progress. Sometimes, seeing that in onesself is the hardest thing to perceive.
Seth:
Now, on to the error trapping portion. Would I be correct that I should pass the table name from the code that I already have in the form's on_load event to a separate Connected() function and do the error trapping in that?
I'm afraid I'm not really sure which part you're talking about here. Generally error handling is done within each procedure where it's required. What errors are you interested in trapping? And which code/procedure are we considering?
Dec 11 '12 #18
Seth Schrock
2,965 Expert 2GB
Sorry, I was referring to Z's idea of trying to do a record move and trapping for an error. I was planning on checking for the existence of the file and then if the file existed, doing the move first/move last and trap for an error as post #5 suggested. I believe this fits what you said in post #9, but I could be wrong.

So to be clear, should I make a separate function to do the record move and trapping for an error in the move. My idea was to replace line 19 in the form's On_Load event (second code block of post #15) with something like the following:
Expand|Select|Wrap|Line Numbers
  1. If Exist(strFile) = True Then
  2.      If Connected(.Name) = False Then Exit Sub
  3. Else
  4.      Exit Sub
  5. End If
Connected(strTableName) would then be the separate function that tries to do the record move.
Dec 11 '12 #19
NeoPa
32,556 Expert Mod 16PB
The test need only be to open the table Seth. It does make sense that the logic for all of that be encapsulated within a procedure that has all the error-handling within.

If you were to call this new procedure Connected(), as it seems is your plan, then the code you posted to illustrate is somewhat strange, however. Getting rid of unnecessary and confusing parts, this would boil down to :
Expand|Select|Wrap|Line Numbers
  1. If Exist(strFile) Then Call Connected(.Name)
  2. Exit Sub
If that's what you're looking for then fine.

I would guess, now that you seem to be happy with the answers in this thread to the original question, that it would make most sense to post this part separately.
Dec 11 '12 #20
Seth Schrock
2,965 Expert 2GB
Opening the recordset was part of the answer to my question as given by Z in post 5. I'm just working that portion of the answer now. I am very happy with the part that tests if the database is there, I just need to finish the test of being able to open the recordset to know if I am truly connected.
Dec 11 '12 #21
Seth Schrock
2,965 Expert 2GB
Well, my stab at opening the recordset and trapping the error didn't work too well. I tried copying what MSDN had here, but I'm getting a Run-time error 3219: Invalid Operation on line 8 for some reason. Here is what I have:

Expand|Select|Wrap|Line Numbers
  1. Public Function Connected(strDBName As String) As Boolean
  2. Dim db As DAO.Database
  3. Dim rsTable As DAO.Recordset
  4.  
  5. 'On Error GoTo Err_SetValue
  6.  
  7. Set db = CurrentDb
  8. Set rsTable = db.OpenRecordset(strDBName, dbOpenTable)
  9.  
  10. Connected = True
  11.  
  12. Err_SetValue:
  13.     Connected = False
  14.  
  15. End Function
Dec 11 '12 #22
NeoPa
32,556 Expert Mod 16PB
Nothing obviously wrong with the code except line #5 is a comment and rsTable is never closed. In the circumstances, we need to know the exact value of strDBName at the time the error occurs, as well as whether or not this value (which should reflect a table of course, and not a database as the name implies) is the name of a table you expect to be valid or not. If you are testing a remote database then you need a table linked to that database in order for the test to be of use to you. This begs the question - Is it really sensible to attempt to set up a dummy link, just to test the values of a link you already have?

This all seems a little confused, if I'm honest. While technically you can continue to progress this in this thread, it is only making a long and complicated thread longer and more complicated. My original advice still holds. From experience I can say this is not making the thread any more helpful or easy to work within. Nevertheless, I'll leave it in your hands. At least for now.
Dec 12 '12 #23
Seth Schrock
2,965 Expert 2GB
Okay. I will split it out and put a link in here for future reference.

Edit: Error opening a recordset

Here is the link to the new thread.
Dec 12 '12 #24

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Bob C. | last post by:
When I migrated my tables to SQL Server I needed a way to overcome the slow performance of the Find method on my recordsets. Although this can be done by accessing the table directly using dao and...
5
by: Andy Davis | last post by:
I have a two tables, Users and Requests, the latter which displays the daily log on's of 20 users. Both tables are related by a one-to-many relationship in the Requests table via the UserId field....
2
by: Merja | last post by:
Is there a quick and easy way to locate tables/querys in the relationships view? If you have over 100 tables in your database it usually takes a long time before you find something in the...
1
by: bitwxtadpl | last post by:
-First I add and relate the tables A and B. Create a new instance of TableA Add TableA to DataSetX Create a new instance of TableB Add TableB to DataSetX Create a relation between TableA and...
9
by: cj | last post by:
I'm trying to forge ahead with Visual Basic .Net but recently I've suffered several major set backs in demonstrating VB is the future and we should move from Visual FoxPro. I really need to find...
0
by: Jim | last post by:
Does anyone know where I can find a 'connected component' image processing function? I don't see it in numarray or scipy core... In matlab the function is called bwlabel(bw,4); and step 8 on...
53
by: Alan Silver | last post by:
Hello, I understand the issue that tables should be used for tabular data and not for layout, but I would like some clarification as to exactly what constitutes tabular data. For example, if...
5
by: WP | last post by:
Hello, I need to communicate with a db2 database from a java program and this java program needs to check which "user tables" there are. I came up with the following query which I tried in Control...
1
by: Andrea | last post by:
I have a data structure composed with a dataset with multiple tables connected through relationship. I'm using different table adapters of the dataset for load table, but when i use the select...
5
by: AiVrea | last post by:
Hi, I have 2 access tables (table1, table2), table2 is related to the field "Nr Crt"(which is an AutoNumber) in table1 in a "one-to-many" relation. Table1 is connected to the vb form to textboxes,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.