473,387 Members | 1,512 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.

Inlavid Operation ereror when refresh table link

I have an Access 2000 application that uses the following function to
re-link my tables when I switch from my Current back end to a Dummy
back end. I also use it to refresh my links.

Function CreateODBCLinkedTables() As Boolean
On Error GoTo CreateODBCLinkedTables_Err
Dim strTblName As String, strConn As String
Dim db As DataBase, rs As Recordset, tbl As TableDef
Dim DatabaseName As String
Dim ServerName As String
Dim DSNName As String
DatabaseName = Forms![frmLINK TABLES]![DatabaseName]
ServerName = Forms![frmLINK TABLES]![ServerName]
DSNName = Forms![frmLINK TABLES]![DSNName]
If Len(DatabaseName) < 2 Or Len(ServerName) < 2 Or
Len(DSNName) < 2 Then
MsgBox ("Error - wrong name ?")
Exit Function
End If

' ---------------------------------------------
' Register ODBC database(s)
' ---------------------------------------------
Set db = CurrentDb
Set rs = db.OpenRecordset("tblODBCDataSources")
With rs
While Not .EOF
DBEngine.RegisterDatabase DSNName, _
"SQL Server", _
True, _
"Description=VSS - " & DatabaseName & _
Chr(13) & "Server=" & ServerName & _
Chr(13) & "Database=" & DatabaseName
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = rs("LocalTableName")
' display what we are linking
Forms![frmLINK TABLES]![linking] = rs("LocalTableName")
Forms![frmLINK TABLES].Repaint
strConn = "ODBC;"
strConn = strConn & "DSN=" & DSNName & ";"
strConn = strConn & "APP=Microsoft Access;"
strConn = strConn & "DATABASE=" & DatabaseName & ";"
strConn = strConn & "UID=" & rs("UID") & ";"
strConn = strConn & "PWD=" & rs("PWD") & ";"
strConn = strConn & "TABLE=" & rs("ODBCTableName")
db.TableDefs.Delete strTblName
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _

strConn)
db.TableDefs.Append tbl

rs.MoveNext

Wend
End With
CreateODBCLinkedTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinkedTables_End:
Exit Function
CreateODBCLinkedTables_Err:
MsgBox Err.Description, vbCritical, "MyApp"
Resume CreateODBCLinkedTables_End
End Function
For no apparent reason, whenever I run the function I get the error
message "Invalid Operation" displayed in a dialog box when one of
my forms is opened. By going to the Tables view I can look at the
data in the table that the form is based on and no errors occur. If I
delete the link to the table, then re-link the table using File, Get
External Data, the application works perfectly.
More details:
1. This code in this application has worked perfectly for years. NO
changes have been made. Even if I go back to an old backup copy of my
application I get the same problem.

2. This code currently works perfectly in all of my other applications.

Even ones that use the table in question here.

3. I get the error even if I just refresh the link to my Current back
end.

4. This occurs on other computers, not just my computer. I am using XP

and have been for quite a while.

I thought my database was corrupt so I imported everything into a blank
database. The problem still exists.

Any help would be appreciated.
Lisa

Jun 21 '06 #1
7 4039
Lisa wrote:
I have an Access 2000 application that uses the following function to
re-link my tables when I switch from my Current back end to a Dummy
back end. I also use it to refresh my links.

Function CreateODBCLinkedTables() As Boolean
On Error GoTo CreateODBCLinkedTables_Err
Dim strTblName As String, strConn As String
Dim db As DataBase, rs As Recordset, tbl As TableDef
Dim DatabaseName As String
Dim ServerName As String
Dim DSNName As String
DatabaseName = Forms![frmLINK TABLES]![DatabaseName]
ServerName = Forms![frmLINK TABLES]![ServerName]
DSNName = Forms![frmLINK TABLES]![DSNName]
If Len(DatabaseName) < 2 Or Len(ServerName) < 2 Or
Len(DSNName) < 2 Then
MsgBox ("Error - wrong name ?")
Exit Function
End If

' ---------------------------------------------
' Register ODBC database(s)
' ---------------------------------------------
Set db = CurrentDb
Set rs = db.OpenRecordset("tblODBCDataSources")
With rs
While Not .EOF
DBEngine.RegisterDatabase DSNName, _
"SQL Server", _
True, _
"Description=VSS - " & DatabaseName & _
Chr(13) & "Server=" & ServerName & _
Chr(13) & "Database=" & DatabaseName
' ---------------------------------------------
' Link table
' ---------------------------------------------
strTblName = rs("LocalTableName")
' display what we are linking
Forms![frmLINK TABLES]![linking] = rs("LocalTableName")
Forms![frmLINK TABLES].Repaint
strConn = "ODBC;"
strConn = strConn & "DSN=" & DSNName & ";"
strConn = strConn & "APP=Microsoft Access;"
strConn = strConn & "DATABASE=" & DatabaseName & ";"
strConn = strConn & "UID=" & rs("UID") & ";"
strConn = strConn & "PWD=" & rs("PWD") & ";"
strConn = strConn & "TABLE=" & rs("ODBCTableName")
db.TableDefs.Delete strTblName
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _

strConn)
db.TableDefs.Append tbl

rs.MoveNext

Wend
End With
CreateODBCLinkedTables = True
MsgBox "Refreshed ODBC Data Sources", vbInformation
CreateODBCLinkedTables_End:
Exit Function
CreateODBCLinkedTables_Err:
MsgBox Err.Description, vbCritical, "MyApp"
Resume CreateODBCLinkedTables_End
End Function
For no apparent reason, whenever I run the function I get the error
message "Invalid Operation" displayed in a dialog box when one of
my forms is opened. By going to the Tables view I can look at the
data in the table that the form is based on and no errors occur. If I
delete the link to the table, then re-link the table using File, Get
External Data, the application works perfectly.
More details:
1. This code in this application has worked perfectly for years. NO
changes have been made. Even if I go back to an old backup copy of my
application I get the same problem.

2. This code currently works perfectly in all of my other applications.

Even ones that use the table in question here.

3. I get the error even if I just refresh the link to my Current back
end.

4. This occurs on other computers, not just my computer. I am using XP

and have been for quite a while.

I thought my database was corrupt so I imported everything into a blank
database. The problem still exists.

Any help would be appreciated.
Lisa


Since I can't duplicate your situation easily I'll offer a shot in the
dark. My best guess of the situation given the symptoms is that a
timing issue has developed due to network access or code being slowed
down. I suspect particularly the section of code:

db.TableDefs.Delete strTblName
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _
strConn)
db.TableDefs.Append tbl

If the code continues on after the Delete too quickly (i.e.,
equivalently, the Delete happens too slowly), there's a possibility
that the Append tries to start before the Delete has completed. Such a
situation could cause the unusual effects you're seeing. The "Invalid
Operation" would then be an unusual result of the Append starting
before the Delete is finished. Try putting a loop of maybe 1000
DoEvents after the Delete statement to see if the timing has become an
issue.

Concerning Details:

1) This theory would account for this behavior.

2) This theory would account for this behavior if the code in the
offending database is running slower than in the other databases. For
example, I have seen situations where the number of controls on a form
alone can slow down combobox rowsource query evaluation.

3) This theory would account for this if you're using the same code to
refresh the link.

4) Same as 2)

If that doesn't work, perhaps we can find another way to isolate the
problem.

Hope this helps,

James A. Fortune
CD********@FortuneJames.com

What works for blogs and news we think actually works for a much wider
set of scenarios... RSS is actually a very interesting way of not only
subscribing to news and blogs, but actually subscribing to files as
well... This idea about subscription is actually a very powerful
concept and actually applies to a lot of file concepts... At the end of
the day, RSS is just XML. -- Amar Gandhi, PDC05, DAT320

Jun 21 '06 #2
On 21 Jun 2006 10:42:45 -0700, CD********@FortuneJames.com wrote:

Hi James,
I disagree with your first point, where you are saying the Append
tries to start before the Delete is finished.
Consider this program fragment:
x=10
x=20
msgbox x
Are you saying in some rare cases x=20 may execute before x=10 is
finished, after which the x=10 line finishes, so the end result is
that the messagebox would show 10? Not likely.
Timing issues only occur if asynchronous processes are involved,
meaning a "shoot-and-forget" type of process that spins off on its own
while the rest of the program continues to execute. Something like
this pseudo-code:
Shell "c:\do_something.bat"
Print "c:\do_something_result_file.txt"
Shell starts an async process, so the Print statement can execute
before the batch file has created the file, causing a FileNotFound
error.
Tabledef.Delete is not an async method.

Back to the code at hand, I think that after Tabledefs.Delete, you
need to run:
Tabledefs.Refresh
And do this again after the Tabledefs.Append line.
The reason is that for performance reasons Access does not refresh the
Tabledefs collection until you forces it to.

I'm not sure why this has never been a problem for the OP. Still I
think it's a good idea to fix the code and try again.

<clip>

Since I can't duplicate your situation easily I'll offer a shot in the
dark. My best guess of the situation given the symptoms is that a
timing issue has developed due to network access or code being slowed
down. I suspect particularly the section of code:

db.TableDefs.Delete strTblName
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _
strConn)
db.TableDefs.Append tbl

If the code continues on after the Delete too quickly (i.e.,
equivalently, the Delete happens too slowly), there's a possibility
that the Append tries to start before the Delete has completed. Such a
situation could cause the unusual effects you're seeing. The "Invalid
Operation" would then be an unusual result of the Append starting
before the Delete is finished. Try putting a loop of maybe 1000
DoEvents after the Delete statement to see if the timing has become an
issue.

Concerning Details:

1) This theory would account for this behavior.

2) This theory would account for this behavior if the code in the
offending database is running slower than in the other databases. For
example, I have seen situations where the number of controls on a form
alone can slow down combobox rowsource query evaluation.

3) This theory would account for this if you're using the same code to
refresh the link.

4) Same as 2)

If that doesn't work, perhaps we can find another way to isolate the
problem.

Hope this helps,

James A. Fortune
CD********@FortuneJames.com

What works for blogs and news we think actually works for a much wider
set of scenarios... RSS is actually a very interesting way of not only
subscribing to news and blogs, but actually subscribing to files as
well... This idea about subscription is actually a very powerful
concept and actually applies to a lot of file concepts... At the end of
the day, RSS is just XML. -- Amar Gandhi, PDC05, DAT320


Jun 22 '06 #3
Tom van Stiphout wrote:
On 21 Jun 2006 10:42:45 -0700, CD********@FortuneJames.com wrote:

Hi James,
I disagree with your first point, where you are saying the Append
tries to start before the Delete is finished.
Consider this program fragment:
x=10
x=20
msgbox x
Are you saying in some rare cases x=20 may execute before x=10 is
finished, after which the x=10 line finishes, so the end result is
that the messagebox would show 10? Not likely.
Timing issues only occur if asynchronous processes are involved,
meaning a "shoot-and-forget" type of process that spins off on its own
while the rest of the program continues to execute. Something like
this pseudo-code:
Shell "c:\do_something.bat"
Print "c:\do_something_result_file.txt"
Shell starts an async process, so the Print statement can execute
before the batch file has created the file, causing a FileNotFound
error.
Tabledef.Delete is not an async method.

Back to the code at hand, I think that after Tabledefs.Delete, you
need to run:
Tabledefs.Refresh
And do this again after the Tabledefs.Append line.
The reason is that for performance reasons Access does not refresh the
Tabledefs collection until you forces it to.

I'm not sure why this has never been a problem for the OP. Still I
think it's a good idea to fix the code and try again.

<clip>


Tom,

My speculation doesn't make a lot of sense to me either but I have seen
Access code turn some kinds of what I expected to be non-asynch tasks
into asynch tasks, perhaps through some kind of code optimization.
Plus, your x = 10 program fragment doesn't match the kind of situation
where Access tries to be so helpful with code execution. I am only
trying to find some reason that takes into account what the OP
experienced. Your Tabledefs.Refresh idea is a good one, but doesn't
make sense of the situation either. Perhaps a Tabledef.Refresh will
also force Tabledef.Create to finish synchronously. I was especially
looking for some reason for a Tabledef to be created, yet be invalid.
It seems that a Tabledef not being refreshed is not as likely as code
optimization to invalidate a Tabledef, but I don't know for sure.
Let's see what the OP discovered.

James A. Fortune
CD********@FortuneJames.com

3D Sidewalk Artist:
http://en.wikipedia.org/wiki/Julian_Beever

Jun 22 '06 #4
Thanks guys. I will give these ideas a try and keep you posted.
Lisa

Jun 22 '06 #5
Tried the above options both together and seperately with no luck.
This is the code with both fixes at the same time:

db.TableDefs.Delete strTblName
'new code to fix Invalid Operation error
Dim counter As Integer
Do While counter < 1020 ' Inner loop.
counter = counter + 1 ' Increment Counter.
If counter = 1000 Then ' If condition is True.
Exit Do ' Exit inner loop.
End If
Loop
'also added following line
db.TableDefs.Refresh
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _
strConn)
db.TableDefs.Append tbl
'also added following line
db.TableDefs.Refresh

Also, the problem does not seem intermittent. Once it is broken the
only way to fix it is to re-link the table manually through File, Get
External Data, Link Tables.

Thanks for your help so far,
Lisa

Jun 22 '06 #6
On 22 Jun 2006 10:26:43 -0700, "Lisa" <lb***@sierrapine.com> wrote:

If your purpose of the loop was to wait a while, remember that you
need to yield the CPU or you will not achieve the goal of allowing
another operation to use the CPU and finish up:

Dim counter As Integer
Counter=0
Do
Counter = Counter + 1
If Counter >= 1000 Then Exit Do
DoEvents 'Yield the CPU
Loop

Personally I like loops that run for a certain time better, e.g. using
the Windows API GetTickCount.
Or call the Windows API Sleep.

-Tom.
Tried the above options both together and seperately with no luck.
This is the code with both fixes at the same time:

db.TableDefs.Delete strTblName
'new code to fix Invalid Operation error
Dim counter As Integer
Do While counter < 1020 ' Inner loop.
counter = counter + 1 ' Increment Counter.
If counter = 1000 Then ' If condition is True.
Exit Do ' Exit inner loop.
End If
Loop
'also added following line
db.TableDefs.Refresh
Set tbl = db.CreateTableDef(strTblName, _
dbAttachSavePWD, rs("ODBCTableName"), _
strConn)
db.TableDefs.Append tbl
'also added following line
db.TableDefs.Refresh

Also, the problem does not seem intermittent. Once it is broken the
only way to fix it is to re-link the table manually through File, Get
External Data, Link Tables.

Thanks for your help so far,
Lisa


Jun 23 '06 #7
Tom van Stiphout wrote:
On 22 Jun 2006 10:26:43 -0700, "Lisa" <lb***@sierrapine.com> wrote:

If your purpose of the loop was to wait a while, remember that you
need to yield the CPU or you will not achieve the goal of allowing
another operation to use the CPU and finish up:

Dim counter As Integer
Counter=0
Do
Counter = Counter + 1
If Counter >= 1000 Then Exit Do
DoEvents 'Yield the CPU
Loop

Personally I like loops that run for a certain time better, e.g. using
the Windows API GetTickCount.
Or call the Windows API Sleep.

-Tom. From the A97 Help file for DoEvents Function:


"For long-running processes, yielding the processor is better
accomplished by using a Timer or delegating the task to an ActiveX EXE
component. In the latter case, the task can continue completely
independent of your application, and the operating system takes care of
multitasking and time slicing."

GetTickCount looks promising. Simply running code for enough time
should work just as well as using DoEvents. This suggests that using
wisdom about the order of doing things can often eliminate the need for
a DoEvents or GetTickCount loop entirely. Within a single procedure a
DoEvents loop seems to solve timing problems such as those associated
with code not waiting for database object interaction to complete. I
tried the Sleep API function for that situation long ago and, IIRC, it
was totally ineffective. It seemed to stop the database object
interaction also. The Sleep function should resolve timing issues
involving code running in totally different threads. I speculate that
this interaction stopping behavior, if true, indicates that VBA running
some database object interaction tasks asynchronously for speed
optimization does the interaction task within the same thread since the
Sleep function is only supposed to suspend the current thread. Note
that an argument to the Sleep function of 0 is rumored to cause the
thread to relinquish the remainder of its time slice to any other
thread of equal priority, but I have not experimented with putting
Sleep(0) in a loop, at least not knowingly :-). I hate to do so much
speculating but I don't know enough about how VBA handles database
object interaction to speak authoritatively about it. I apologize in
advance if my recollection of the Sleep function behavior is incorrect.

James A. Fortune
CD********@FortuneJames.com

Jun 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: FrogyWill | last post by:
Hi Below is a great script I found to make charts. The value are in form objects. I am changing the hidden fields to drop down list with multiple values, so that the users can choose what data...
4
by: beta2000 | last post by:
I want to try Refresh() again if the first Refresh() fails. Code: try { try { Refresh(); // ... first attempt... } catch(...) {
7
by: KathyK | last post by:
Hi and thanks in advance! I have an Access 2000 front end and a SQL Server 2000 back end. When the Access main form opens I run code that logs the user on to the SQL server using the generic read...
10
richardhodge
by: richardhodge | last post by:
I am a VB6 database programmer and have run into a small problem. The company I work for primarily uses Microsoft Access 2000 for the database that is the back end for our software. Well the...
7
by: ApexData | last post by:
Hello I currently Link the FE/BE using the LinkTables Option and the Linked Table Manager. Any time I need to move the BE to another location, I have to go through this process over again. I...
4
by: Bob | last post by:
Hi all, I've got a table that I've imported and it has junk at the top of the table, so after import I run a delete query to remove the junk lines then I'm left with the field names I want for...
8
by: pukhton | last post by:
I am getting a yellowish block on Refresh statement under Medication_Name_AfterUpdate, and If i take it out it comes to the End sub... dont know what is goign on? This is what I have under Text52...
64
by: Mika | last post by:
Hello, we understand you guys may be able to help. We have a page which has been working great for over a year and gets many hits. However recently something got changed that we cannot seem to...
3
by: Roger | last post by:
I've got a sql server view, that I link as a table in access97 after initial linking, I can open the table and add more records (ie. the >* vcr button is enabled) the connect string for the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.