473,386 Members | 1,860 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,386 software developers and data experts.

Set Form RecordSource Property to Table of External Database

I am trying to write an MS ACCESS VBA application that has the code in one *.accdb file and the data itself in a separate *.accdb file.

I am trying to set the RecordSource property of a SplitForm to all the elements of a table (Table1) in the external database, but don't know how to properly express the combination <DBname>.<TableName> for that function.

The following Code does NOT work, but almost does

Expand|Select|Wrap|Line Numbers
  1.     Dim myQuery As String
  2.     myQuery = "Select * From Table1"
  3.     Set gSplitViewForm = New Form_TestTable
  4.     gSplitViewForm.RecordSource = myQuery
  5.     gSplitViewForm.Visible = True
  6.     gSplitViewForm.SetFocus
  7.  
Error Message:
The record soruce "Select * from Table1" specified on this form or report does not exist.

The error message I'm getting basically says that Table1 doesn't exist. I see this as a problem naming Table1 as part of the external database. How do I do that?

Thanks... TGlagowski
Mar 2 '12 #1

✓ answered by TheSmileyCoder

The standard approach would be to use a linked table, allthough I do believe its possible to use a query in the way you have outlined. But before complicating matters I would like to make sure that you have a good reason to not use a linked table?

Linked tables function pretty much like normal tables in access. You can open them directly from your frontend, and manipulate them. The only difference is that they are located in your backend instead.

17 7624
TheSmileyCoder
2,322 Expert Mod 2GB
The standard approach would be to use a linked table, allthough I do believe its possible to use a query in the way you have outlined. But before complicating matters I would like to make sure that you have a good reason to not use a linked table?

Linked tables function pretty much like normal tables in access. You can open them directly from your frontend, and manipulate them. The only difference is that they are located in your backend instead.
Mar 2 '12 #2
My understanding of the error message is that the system doesn't think that Table1 exists... and it doesn't within the *.accdb that contains the VBA code... What I need is a way to express something like... ExternalDBname.Table1 when I set the Form.RecordSource property. Do you know what the syntax would be for this?
TGlagowski
Mar 3 '12 #3
Mihail
759 512MB
Try to create the query using the Query design view interface.
I am sure that you can't because you can't add Table1 to the design window. And you can't do that because (you are almost right) the system know that is not a table named Table1.

So, as Smiley teach you in post #2, link the Table1 to the current database. After you do that all will be Ok.
If you don't know how to inform us about Access version you use and someone will teach you.
Mar 3 '12 #4
Using Win 7 64 Ultimate
Using Office 2010 MS Access
Is the best keyword for help "Linked Tables" ?
I'll take a look...
From what I read from you... the approach is...
Link Table1 from the external database to the CurrentDatabase...
Is that correct?
Mar 3 '12 #5
Mihail
759 512MB
Ok.
I use 2007 but I think that the interface is the same in 2010.

So:
1) In your current database go to External Data tab of ribbon.
2) Under Import section click Access.
This action will open the Get External Data window.
3) Choose Link to data source by creating a linked table.
4) Click Browse...
This action will open the File Open window
5) Browse the database where you have Table1, select it and click Open.
This action will open the Link Tables window.
6) Select your Table1 then click OK.

That is all.

Good luck !
Mar 3 '12 #6
NeoPa
32,556 Expert Mod 16PB
TGlagowski:
Link Table1 from the external database to the CurrentDatabase...
Is that correct?
Yes it is.
Mar 3 '12 #7
Thanks everyone for your responses.
The key information that you provided was the concept of "Linked Table".
Using that keyword, I searched the Microsoft Office Help files.
The article entitled TableDef.SourceTableName Property (DAO) had the answers.

My working code example follows.
An external database "Example1.accdb" has a table "Table1" with 3 fields: ID (autonumber), DBKey (text), DBValue (text).
The goal is to associate a SplitView Form recordsource with this table, also the corresponding fields. This form has the same fields as the DB Table.

If anyone has any comments about how this code can be improved,
please pass them along.
Expand|Select|Wrap|Line Numbers
  1.     Dim myDBFileName As String
  2.     Dim myLinkedTableDef As TableDef
  3.     Dim myTableName As String
  4.     Dim myQuery As String
  5.  
  6.     Rem Setup Linked Table
  7.     myDBFileName = "Example1.accdb"
  8.     myTableName = "Table1"
  9.     Set myLinkedTableDef = CurrentDb.CreateTableDef(myTableName)
  10.     myLinkedTableDef.Connect = ";DATABASE=" & myDBFileName
  11.     myLinkedTableDef.SourceTableName = myTableName
  12.     CurrentDb.TableDefs.Append myLinkedTableDef
  13.  
  14.     Rem Open Trace View Form (TestTable)
  15.     Set gSplitViewForm = New Form_TestTable
  16.     myQuery = "Select * From " & myTableName
  17.  
  18.     Rem Set RecordSource for Form
  19.     gSplitViewForm.RecordSource = myQuery
  20.  
  21.     Rem Set ControlSource for Fields in Form
  22.     gSplitViewForm.ID.ControlSource = "ID"
  23.     gSplitViewForm.DBKey.ControlSource = "DBKey"
  24.     gSplitViewForm.DBValue.ControlSource = "DBValue"
  25.  
  26.     Rem Set Properties of Form
  27.     gSplitViewForm.Visible = True
  28.     gSplitViewForm.SetFocus
  29.  
  30.     Rem Clean Up Linked TableDef
  31.     CurrentDb.TableDefs.Delete myTableName
  32.  
- TGlagowski
Mar 3 '12 #8
NeoPa
32,556 Expert Mod 16PB
My only comment is to suggest that post #2 might be a candidate for Best Answer in light of your comments ;-)
Mar 3 '12 #9
Mihail
759 512MB
The code seems to be Ok.
But why all that work to do, via VBA, what Access can do in simple steps (after you link table as I show you) ?

Certainly is not a problem with the best answer. I know very well Smiley and his skills :) .
This is a conceptual problem.
May I ask you how you plan to proceed if you wish, later, to define a query based on this table ?
Mar 3 '12 #10
Answers for MiHail.

Why VBA?
This discussion was about a prototype example which models the larger routine in a real application. I needed to know how to go about setting the Form.recordsource property to the table of an external database, also the Control.controlsource properties of the relevant controls.
I was unfamiliar with the concept of "linked table".
Yes, I agree that SmileyCoder had the first and best answer.
Once I understood that "linked table" was the key, I found my way.

I am building an MS ACCESS VBA application which prints Amateur Radio QSL cards from a Log Database of Radio Contacts. It performs a number of other functions as well. This log is created by yet another application known as Ham Radio Deluxe or HRD which uses the MS ACCESS database as a repository.

When printing a card, I want to query the database for the contacts that optionally match the callsign, date, time, frequency, band, mode. A query will be constructed from the supplied information to be run against the database. A list of matches will be displayed in the SplitForm and the one selected will be used for printing using VISIO. Simple MS ACCESS alone won't do the job, so VBA is used.

Defining a Query?
Another form (not shown in the prototype example) has a list of fields some of which are mandatory and some of which are optional as contribution to the query that will be synthesized for use against the database. The expected reply is a set of zero or more records from the table, one of which will be selected for printing the QSL card. If none are returned, the query will have to be reformulated.

FYI, check www.arrl.org for introduction to amateur radio, search on QSL cards as well, if you are interested.

Thanks...
TGlagowski (W1TR)
Mar 3 '12 #11
TheSmileyCoder
2,322 Expert Mod 2GB
I am starting to guess from your answers that your probably an experienced programmer, but that you do not have that much experience in using Access. This is not meant to offend you, I am just trying to understand you, and your area of expertise.

Now the thing about using access, and what helps make it a great tool, is that can be used as a combined client/server tool. It can work both as the client as well as the database. This can be very usefull during rapid development, proof of concept stages of a project. And then later, should one choose one can move the backend (the data) out of the access file, and either into a seperate access file (as in your case) or onto a SQL server or similar.

You can setup the linked tables once and for all, directly through the access user interface (or by code as you do). Once this is done you can use the linked table in pretty much the same way as you would any internal table, except that to modify a linked tables design, you need to open up the backend, and have exclusive access to the backend table.

There is nothing wrong with creating the link through code as you do, but unless you have a good reason to use such an approach I dont recommend it. You simply end up adding extra VBA coding, maintenance and complexity to your project.

To add a link to a table in AC2010, you click External Data, and in the group "Import & Link" you click Access. This starts a wizard, and you need to make sure that the first option group you see is set to "Link to the data source by creating a linked table".


In most of the applications I have developed using Access, I have a setup where each individual user gets his/her copy of the frontend database. A frontend database will contain all my forms, queries, reports and code. The backend will be placed on the network drive, and contain all the tables (and nothing else). All the tables seen in the frontend, are actually linked tables. You can in this scenario consider the frontend a Client, and the backend the server.

I hope this helped to clarify some of the issues you are facing.
Mar 4 '12 #12
Answers to SmileyCoder...
You have it right...
I am very experienced in general, coding in FORTRAN 2 and 4 since 1964... IBM 1620, 1401, 7040, PDP-5 etc...
MEEE embedded, VLSI, kernel mode / PhD CS and knowledgeable about C++ and its many predecessors. Taught EECS SW/ENGR at WSU in the 90s.
And...INexperienced in Access and some of the Office Suites and some of the newer programming environments.

The reason I want to do the linked table in VBA vs MS Access design interface is that I have MANY databases (several dozen) that I want to be able to dynamically link to, also several different TYPEs of database from different applications (3 or so with different schemas)...

This is NOT a commercial application or even shareware (for the moment anyway) and isn't high quantity of transactions like the financial business, etc...

My lack of knowledge is of the VBA object model for the various Office Suite applications, etc. So I don't know what the levers (API functions) that are available to make things happen !
Online / WEB information has become much better in the last few years but I just haven't had time to read it all.

The biggest thing you folks helped me with was to recognize that "linked tables" were the key to what I was trying to do, and then I looked up online help regarding that and proceeded to have success. Thank you very much for having patience with an old fart!

TGlagowski / W1TR
Mar 4 '12 #13
Mihail
759 512MB
I think that this link also can be a help for you:
http://bytes.com/topic/access/insigh...ba#post3525573
Mar 4 '12 #14
TheSmileyCoder
2,322 Expert Mod 2GB
Hi TGlagowski
Thank you for bearing with me, as I continue to drum on you, even though you seem satisfied with the solution you have. I just wanted to make sure you were using the code for the right reasons, instead of simpling setting up the linked tables from the start.
Your expressed requirements of dynamically linking backends is such a case, and I do feel satisfied that your doing it for the "right" reasons and not a lack of understanding of the mechanics of access now.




I just want to add one more bit of information to the pack here, as I do believe its relevant to your case, and might simplify it for you.

I finally managed to find a article I was looking for. I remember seing somewhere that you can actually write the database path+name directly into the SQL statement, the problem was being able to get the right collection of words for googling it, so I could find it again.

Try taking a look at this article by Crystal Long, posted at Allan Browne's webpage. http://allenbrowne.com/binary/Access...Chapter_06.pdf Especially look at page 2 of the pdf.

I tried it myself and were able to use like so with a query I created and wrote the SQL directly:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM tbl_ClientVersion in 'C:\Test\Test.mde'
I noticed upon returning to design view, that the query actually has a property called Source Database, in which my "C:\Test\Test.mdb" was now actually written.
Mar 5 '12 #15
To SmileyCoder
The SQL statement was probably what I was really looking for in the first place, but there is a lot I don't know about VBA programming for MS Office, particularly MS Access...
Perhaps I should read more about the finer points of SQL within VBA.
I can see that I probably don't want to have the overhead of setting up and tearing down the linked table per query but rather per session and I'll deal with that as the application evolves.

Next Challenge(s) for me...
1) Sending / Receiving ASCII bytes via Serial I/O (COM Ports) to communicate with radio equipment.
2) Doing some "real time" kinds of things, timer driven events, etc.
3) Multi threading.
4) Calling external DLLs... probably written in C or C++.
I HAVE done these things in MFC / C++ but not in VBA (without MS Visual Studio).
TGlagowski
Mar 5 '12 #16
NeoPa
32,556 Expert Mod 16PB
You will find information about the SQL In keyword from Finding Jet SQL Help.
Mar 5 '12 #17
As a matter of showing the code for the revised example with the SQL "IN" clause, here it is for other interested parties:

Expand|Select|Wrap|Line Numbers
  1.     Dim myDBFilename As String
  2.     Dim myTableName As String
  3.     Dim myQuery As String
  4.  
  5.     Rem Select Database
  6.     Rem   Database has one table: "Table1"
  7.     myDBFilename = "C:\Example1.accdb"
  8.  
  9.     Rem Select Table
  10.     Rem   Table1 has 3 fields:
  11.     Rem     ID (Autonumber)
  12.     Rem     DBKey (text)
  13.     Rem     DBValue (text)
  14.     myTableName = "Table1"
  15.  
  16.     Rem Construct Query
  17.     myQuery = "Select * From " & myTableName & " in '" & myDBFilename & "'"
  18.  
  19.     Rem Open Split View Form
  20.     Rem   Form has same 3 fields as DB Table1
  21.     Set gSplitViewForm = New Form_TestTable
  22.  
  23.     Rem Set RecordSource for Form
  24.     gSplitViewForm.RecordSource = myQuery
  25.  
  26.     Rem Set ControlSource for Fields
  27.     gSplitViewForm.ID.ControlSource = "ID"
  28.     gSplitViewForm.DBKey.ControlSource = "DBKey"
  29.     gSplitViewForm.DBValue.ControlSource = "DBValue"
  30.  
  31.     Rem Set Form Properties
  32.     gSplitViewForm.Visible = True
  33.     gSplitViewForm.SetFocus
  34.  
TGlagowski
Mar 5 '12 #18

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

Similar topics

1
by: Martin_Hurst | last post by:
Is it possible to establish a connection from within a postgres database to another external database on the network, i.e., not a java or scripting connection, BUT by some type of table definition...
4
by: Mr. Bungle | last post by:
Just when I thought I knew everything... Does anyone know why sometimes when creating a subform, the RecodSource property is totally unavailable, this appears to happen when you don't use the...
3
by: Daniel M | last post by:
I'm building a medium-scale data-entry web application, which involves creating data entry forms, record listings and detail screens for lots of database tables. Rather than designing a series...
3
by: Alan | last post by:
I'm having brain fade today... Can the recordsource in a Form's properties do more than one select statement based on criteria? On a popup subform I'm trying to display matching records from a...
3
by: Number 5 | last post by:
Hi, I was wondering if there is a way to set the RecordSource property of a form in my front-end as a table in my back-end. Another additional hurdle is that the table name is a variable, because...
4
by: Bosconian | last post by:
I have created a form tool page that consists of several dropdowns which allows a user to drill down and select a desired document for viewing in an external window. I need to post the form data to...
5
by: Ian | last post by:
I am trying to: 1. Open a form on the external database 2. Enter a value in a text box on that form I have 1 above working OK using module form “The Access Web”, the module looks like this: ...
0
by: Frank Kolbe | last post by:
I have an access application with a backend Sql Server. All data is passed from the back-end (remote Sql Server) to unbound forms via stored procedure. I then set the recordsource of the form...
1
by: mfaisalwarraich | last post by:
Hi Everybody, I have an external database called Patients.mdb where i made a query called qryAdmissionDetails. now i want to run this query on a report of another database called...
8
by: Lebbsy | last post by:
On clicking a button I want to add data in to tables. The first table captures all information specified in the form and works fine. The second table that does not have a recordsource, captures...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.