473,408 Members | 2,734 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,408 software developers and data experts.

How to Copy a Linked Table to another table

anoble1
245 128KB
I am attempting to copy a linked Global Address List table to a local table with a click of a button.

I am just using some simple code to transfer like I have done in the past with regular tables but when it copies it create/converts the local table there into another linked table just like the "Global Address List" which is what I do not want.

How can I copy that GAL into a Local Table with a button?
Expand|Select|Wrap|Line Numbers
  1. DoCmd.CopyObject , "tblNames", acTable, "Global Address List"
Sep 10 '19 #1
5 4594
twinnyfo
3,653 Expert Mod 2GB
anoble1,

The simplest way to do this (which is not via VBA, which is how I know you want to do it), is simply right-click on your GAL table and select "Convert to local table." I haven't found a good way to do that with VBA. There is this:

Expand|Select|Wrap|Line Numbers
  1. Call DoCmd.SelectObject(acTable, "Global Address List", True)
  2. Call RunCommand(acCmdConvertLinkedTableToLocal)
But I can't get it to actually execute with any of my linked tables. You can give it a try.

Why do you need this table locally? Usually you would want a GAL linked, so that you could receive updates.

==================
Update:
My research points to my failure as the fact that I have some relationships built on my table. If yo have no relationships to your linked table it may just work!
Sep 11 '19 #2
anoble1
245 128KB
I need to query that table and when I query that GAL it takes like over a minute. If I had a button that could be ran every few days to update that Local table from an end user than that would be nice.
Sep 11 '19 #3
twinnyfo
3,653 Expert Mod 2GB
Was that code functional?
Sep 11 '19 #4
ADezii
8,834 Expert 8TB
  1. This is simply in the event that twinnyfo's approach does not work.
  2. I create a Sub-Routine for you that requires only the Name of a Linked Table and it will do the rest.
  3. This Routine will:
    1. Examine the Linked Table to see if it is indeed Linked Table. This is done by checking the Connect Property of a Linked Table.
    2. Assuming it is a Linked Table, extract the Database Path from the Connect String.
    3. Retrieve the Source Table name from the Linked Table.
    4. DELETE the Linked Table. You are not DELETING the Table itself, but simply the Link to it.
    5. Import the Linked Table into the Current Database as a 'Local' Table.
  4. I'm afraid that I may be making matters more complicated than they actually are, so I'll get to he heart of the matter.
  5. Sub-Routine Definition:
    Expand|Select|Wrap|Line Numbers
    1. Public Sub ConvertLinkedToLocal(strLinkedTbl As String)
    2. Dim strConnect As String
    3. Dim strPath As String
    4. Dim strSourceTable As String
    5.  
    6. strConnect = CurrentDb.TableDefs(strLinkedTbl).Connect      'Connect String
    7.  
    8. If InStr(strConnect, "=") = 0 Then
    9.   MsgBox strLinkedTbl & " is not a Linked Table!", vbCritical, "Linked Table Error"
    10.     Exit Sub
    11. Else
    12.   strPath = Mid$(strConnect, InStr(strConnect, "=") + 1)        'Actual DB Path
    13.   strSourceTable = CurrentDb.TableDefs(strLinkedTbl).SourceTableName
    14.  
    15.   DoCmd.DeleteObject acTable, strLinkedTbl
    16.   DoCmd.TransferDatabase acImport, "Microsoft Access", strPath, acTable, _
    17.                          strSourceTable, strLinkedTbl, False
    18. End If
    19. End Sub
  6. Sample Call to Sub-Routine (resulting in the Linked Table tblNames now becoming a Local Table named tblNames.
    Expand|Select|Wrap|Line Numbers
    1. Call ConvertLinkedToLocal("tblNames")
P.S. - I tested the Code using a Linked Table that it is involved in Multiple Relationships in the Back End and it worked fine. I am referring to the Order Details Table of the Northwind Sample Database.
Sep 11 '19 #5
NeoPa
32,556 Expert Mod 16PB
Hi Andy.

If I understand you correctly you'd like an accurate copy of the data within the GAL, as at a certain point in time (so not dynamic as such), to be available in a local table which is refreshed separately from when it's used.

You've already discovered, as I just did to my surprise, that the Paste option to append to existing table is NOT available with DoCmd.CopyObject(). That rather sucks frankly. I was going to find it for you and point out its availability but that didn't happen!

So, the alternative I would suggest, for the populating of this table so that it matches your GAL at the time it's run, would be to use a few fairly simple SQL commands in sqequence.

Before you do any of that create yourself a local copy of the table using Copy/Paste manually from the GAL and set it as a local table. The data doesn't matter for now. For the purposes of clarity I will refer to that table as [tblLocalGAL]. The other (Proper / linked.) I'll call [GAL] for now but you know what it is on your system.

Now, the routine will basically clear the data from [tblLocalGAL] first and then immediately re-populate it from [GAL]. Thus the full set of data is an exact duplicate at that point in time.

NB. I've included some procedures below which I use for ensuring multiple sets of SQL are run together as a single transaction. You're free to use it but you may already have something similar.

Clearing [tblLocalGAL] is very basic SQL :
Expand|Select|Wrap|Line Numbers
  1. DELETE FROM [tblLocalGAL]
After that you create a SQL string to perform the APPEND query and Bob's your uncle. As [tblLocalGAL] has been duplicated by your earlier Copy/Paste from [GAL] it has the same fields available so the SQL will be of the following format (You may have to leave out some Fields from the APPEND if they're created automatically like AutoNumbers but I doubt that in this case.) :
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [tblLocalGAL]
  2.           ( [Field1]
  3.           , [Field2]
  4.           , ...
  5.           , [FieldN])
  6. SELECT      [Field1]
  7.           , [Field2]
  8.           , ...
  9.           , [FieldN]
  10. FROM        [GAL]
Put both SQLs into a single string separated by a semi-colon (;) and pass that to RunSQLsAsTran() and you have a process that will keep your data up-to-date as far as you need depending on when, or how frequently, it's run.

Good luck.

Expand|Select|Wrap|Line Numbers
  1. Private Const conMaxLocks As Long = 9500
Expand|Select|Wrap|Line Numbers
  1. 'RunSQLsAsTran() executes the contents of strSQLs as successive SQL commands
  2. '  separated by semi-colons.  The whole set is treated as a single transaction,
  3. '  so if any one of them fails then all are rolled back.
  4. '  If strMsg is not "" then it prompts the operator first who can proceed or halt.
  5. '  If [frmBusy] exists in your project it will be displayed while the SQLs run.
  6. '  Returns True if it completes successfully.
  7. ' 24/09/2015    Added handling of running out of resources.
  8. ' 25/05/2019    Added ignoring of empty strings.
  9. Public Function RunSQLsAsTran(ByVal strMsg As String, _
  10.                               ByRef strSQLs As String, _
  11.                               Optional dbVar As DAO.Database, _
  12.                               Optional wsVar As DAO.Workspace) As Boolean
  13.     Dim strMode As String, strWhere As String
  14.     Dim varSQL As Variant
  15.  
  16.     If strMsg > "" Then
  17.         strMsg = Replace(strMsg, "%L", vbNewLine)
  18.         If MsgBox(Prompt:=strMsg, _
  19.                   Buttons:=vbOKCancel Or vbQuestion, _
  20.                   TITLE:="RunSQLsAsTran") = vbCancel Then Exit Function
  21.     End If
  22.     'Handle form frmBusy not being available when we open it.
  23.     On Error Resume Next
  24.     Call DoCmd.OpenForm(FormName:="frmBusy")
  25.     DoEvents
  26.     On Error GoTo 0
  27.     If dbVar Is Nothing Then Set dbVar = CurrentDb()
  28.     If wsVar Is Nothing Then Set wsVar = WorkspaceFromDB(dbVar)
  29.     'Allow more resources as this is a transaction.
  30.     Call DBEngine.SetOption(Option:=dbMaxLocksPerFile, Value:=10 * conMaxLocks)
  31.     Call wsVar.BeginTrans
  32.     On Error GoTo ErrorHandler
  33.     'Process all the separate SQL strings passed.
  34.     For Each varSQL In Split(strSQLs, ";")
  35.         If Trim(varSQL) > "" Then _
  36.             Call dbVar.Execute(Query:=varSQL, Options:=dbFailOnError)
  37.     Next varSQL
  38.     'If all OK so far then commit the transaction and requery this form.
  39.     Call wsVar.CommitTrans(dbForceOSFlush)
  40.     'Reset resources to default for normal operation.
  41.     Call DBEngine.SetOption(Option:=dbMaxLocksPerFile, Value:=conMaxLocks)
  42.     RunSQLsAsTran = True
  43.     If FormLoaded(strForm:="frmBusy") Then Call CloseMe(objMe:=Forms("frmBusy"))
  44.     Exit Function
  45.  
  46. ErrorHandler:
  47.     'Handle message first before Err is corrupted.
  48.     strMsg = Replace("This update failed.%L%L" _
  49.                    & "Error %N%L%D%L%L" _
  50.                    & "Please report this problem to Support." _
  51.                    , "%N", Err.Number)
  52.     strMsg = Replace(strMsg, "%D", Err.Description)
  53.     strMsg = Replace(strMsg, "%L", vbNewLine)
  54.     Call wsVar.Rollback
  55.     If FormLoaded(strForm:="frmBusy") Then Call CloseMe(objMe:=Forms("frmBusy"))
  56.     'Reset resources to default for normal operation.
  57.     Call DBEngine.SetOption(Option:=dbMaxLocksPerFile, Value:=conMaxLocks)
  58.     Call MsgBox(Prompt:=strMsg, _
  59.                 Buttons:=vbOKOnly Or vbExclamation, _
  60.                 TITLE:="RunSQLsAsTran")
  61. End Function
Expand|Select|Wrap|Line Numbers
  1. 'FormLoaded determines whether or not a form object is loaded.
  2. Public Function FormLoaded(strForm As String) As Boolean
  3.     FormLoaded = False
  4.     On Error Resume Next
  5.     FormLoaded = (Forms(strForm).Name = strForm)
  6. End Function
Sep 14 '19 #6

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

Similar topics

7
by: Philip Mette | last post by:
I have been searching many postings and I cant seem to find anyone that has this answer so I decided to post. I am using SQL (Transact-SQL).If I have 2 tables with columns...
3
by: Michael Plant | last post by:
Hello one and all. I have a stored table in my database and the form I'm using is based on a query that draws data from my stored table and a linked table. The linked table is a *.txt file. ...
4
by: Wayne Wengert | last post by:
I am trying to create a VB.NET Windows application to move some data from a local Access DB table to a table in a SQL Server. The approach I am trying is to open an OLEDB connection to the local...
3
by: colleen1980 | last post by:
Hi: Can any one please help me when user select 2 dates from DDLDate1 10/09/2006 and DDLDate2 10/12/06 and the name and it close the form. I need to create multiple records in the another table on...
2
by: David - Australia | last post by:
G'day from Australia, I'm hoping some bright spark may be able to help me with this one. I'm sure that it can be done, I've just hit a wall with it. So I'm opening it up. I'm storing student...
1
by: Randoz | last post by:
I recieve an updated excel file with pers information every other week and when I go to import the data into my table the data is not updated for the records that are linked to a course table. I...
3
by: ghost1980 | last post by:
hi there, i am new to ms access and currently i am in need of help. here is my problem i need to help create a form that allow me to update 2 tables. example: PERSON1 (table 1) id name dob...
3
by: shubham rastogi | last post by:
hello guys I want to copy or insert records into the previously created table from another table.. For example I have two tables A and B .... I want to copy or insert records from table B into...
1
by: CSJeep | last post by:
Hello I have a sql 2008 R2 problem that I’ve tried to accomplishing using dynamic sql but, no luck yet. Create Table Events ( PKID int Not Null, TimeStamp Datetime, EventID int, Eventdata...
2
by: Philywilly03 | last post by:
I am attempting to update a table that keeps an inventory using a form that creates records for for a different table. I'm pretty weak on the programming side, but I have been looking around at other...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.