473,770 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Field count in another database

I am trying to count the fields in a queryDef in an external database.

If I run this in the actaal database I get Fields.count = 6 correctly

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim MyDb As Database
Dim MyQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set MyDb = CurrentDb

Set MyQueryDef = MyDb.QueryDefs( "MyQueryNam e")
For i = 0 To MyQueryDef.Fiel ds.Count - 1
...
Next
Exit Sub

ShowObject_Err:
MsgBox Err.Description , vbCritical

End Sub

If I run a similar bit of code in an external database, depending on which
query I am referencing I get either the correct number of filds or 0 -
incorrect

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim AccApp As Access.Applicat ion
Dim OtherDb As Database, MyDb As Database
Dim OtherQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set AccApp = New Access.Applicat ion
Set OtherDb = AccApp.DBEngine .OpenDatabase(D atabaseName, False, False,
Nz(PWD))

Set MyDb = CurrentDb

Set OtherQueryDef = OtherDb.QueryDe fs("MyQueryName ")
For i = 0 To OtherQueryDef.F ields.Count - 1
...
Next
Exit Sub

ShowObject_Err:
MsgBox Err.Description , vbCritical

End Sub

The difference appears to be whether any of the fields call any code ( for
example I use code to build up the names of a family from their individual
names).

Any ideas how to get round the proble. Using Access 2000

Thanks

Phil
Jan 25 '07 #1
10 3226
Replace your 4th line with:
Set MyDb = OpenDatabase("C :\MyFolder\MyFi le.mdb")

Before the exit, close what you opened:
MyDb.Close

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Phil Stanton" <ph**@stantonfa mily.co.ukwrote in message
news:45******** **************@ ptn-nntp-reader02.plus.n et...
>I am trying to count the fields in a queryDef in an external database.

If I run this in the actaal database I get Fields.count = 6 correctly

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim MyDb As Database
Dim MyQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set MyDb = CurrentDb

Set MyQueryDef = MyDb.QueryDefs( "MyQueryNam e")
For i = 0 To MyQueryDef.Fiel ds.Count - 1
...
Next
Exit Sub

ShowObject_Err:
MsgBox Err.Description , vbCritical

End Sub

If I run a similar bit of code in an external database, depending on which
query I am referencing I get either the correct number of filds or 0 -
incorrect

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim AccApp As Access.Applicat ion
Dim OtherDb As Database, MyDb As Database
Dim OtherQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set AccApp = New Access.Applicat ion
Set OtherDb = AccApp.DBEngine .OpenDatabase(D atabaseName, False, False,
Nz(PWD))

Set MyDb = CurrentDb

Set OtherQueryDef = OtherDb.QueryDe fs("MyQueryName ")
For i = 0 To OtherQueryDef.F ields.Count - 1
...
Next
Exit Sub

ShowObject_Err:
MsgBox Err.Description , vbCritical

End Sub

The difference appears to be whether any of the fields call any code ( for
example I use code to build up the names of a family from their individual
names).

Any ideas how to get round the proble. Using Access 2000
Jan 25 '07 #2
Thanks, Allen

The problem is with the OtherDB, not MyDb

The program works fine for ordinary queries in the OtherDB, it is only if
those queries contain calls to functions that I get no fields returned.
I have tried including a reference to the OtherDB in the reference section
of VBA, but that didn't help.

Culpa mea forgot to close the DB.

Any more thoughts?

Thanks

Phil

"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:45******** *************** @per-qv1-newsreader-01.iinet.net.au ...
Replace your 4th line with:
Set MyDb = OpenDatabase("C :\MyFolder\MyFi le.mdb")

Before the exit, close what you opened:
MyDb.Close

--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Phil Stanton" <ph**@stantonfa mily.co.ukwrote in message
news:45******** **************@ ptn-nntp-reader02.plus.n et...
>>I am trying to count the fields in a queryDef in an external database.

If I run this in the actaal database I get Fields.count = 6 correctly

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim MyDb As Database
Dim MyQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set MyDb = CurrentDb

Set MyQueryDef = MyDb.QueryDefs( "MyQueryNam e")
For i = 0 To MyQueryDef.Fiel ds.Count - 1
...
Next
Exit Sub

ShowObject_Err :
MsgBox Err.Description , vbCritical

End Sub

If I run a similar bit of code in an external database, depending on
which query I am referencing I get either the correct number of filds or
0 - incorrect

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim AccApp As Access.Applicat ion
Dim OtherDb As Database, MyDb As Database
Dim OtherQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set AccApp = New Access.Applicat ion
Set OtherDb = AccApp.DBEngine .OpenDatabase(D atabaseName, False, False,
Nz(PWD))

Set MyDb = CurrentDb

Set OtherQueryDef = OtherDb.QueryDe fs("MyQueryName ")
For i = 0 To OtherQueryDef.F ields.Count - 1
...
Next
Exit Sub

ShowObject_Err :
MsgBox Err.Description , vbCritical

End Sub

The difference appears to be whether any of the fields call any code (
for example I use code to build up the names of a family from their
individual names).

Any ideas how to get round the proble. Using Access 2000

Jan 25 '07 #3
Hmm. Yes, I'd forgotten it did that.

If Access can't give the count, It should be possible to parse the fields
from the SQL statement. The example below is incomplete. It just parses the
field list at the commas. You would really need to remove any literals
first, so you don't get caught with things like:
SELECT [LastName] & ", " & [FirstName] AS FullName

Anyway the basic approach would be:

Function FieldCount()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSql As String
Dim varArray As Variant
Dim lngStart As Long
Dim lngEnd As Long

Set db = OpenDatabase("C :\MyPath\MyFile .mdb")
Set qdf = db.QueryDefs("Q uery1")
strSql = qdf.SQL
lngStart = InStr(strSql, "SELECT ")
If lngStart 0& Then
lngStart = lngStart + 7&
lngEnd = InStr(lngStart, strSql, "FROM ")
strSql = Left$(strSql, lngEnd)
strSql = Mid$(strSql, lngStart)
varArray = Split(strSql, ",")
If IsArray(varArra y) Then
FieldCount = UBound(varArray ) + 1&
End If
End If

Set qdf = Nothing
db.Close
Set db = Nothing
End Function
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Phil Stanton" <ph**@stantonfa mily.co.ukwrote in message
news:45******** **************@ ptn-nntp-reader02.plus.n et...
Thanks, Allen

The problem is with the OtherDB, not MyDb

The program works fine for ordinary queries in the OtherDB, it is only if
those queries contain calls to functions that I get no fields returned.
I have tried including a reference to the OtherDB in the reference section
of VBA, but that didn't help.

Culpa mea forgot to close the DB.

Any more thoughts?

Thanks

Phil

"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:45******** *************** @per-qv1-newsreader-01.iinet.net.au ...
>Replace your 4th line with:
Set MyDb = OpenDatabase("C :\MyFolder\MyFi le.mdb")

Before the exit, close what you opened:
MyDb.Close

"Phil Stanton" <ph**@stantonfa mily.co.ukwrote in message
news:45******* *************** @ptn-nntp-reader02.plus.n et...
>>>I am trying to count the fields in a queryDef in an external database.

If I run this in the actaal database I get Fields.count = 6 correctly

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim MyDb As Database
Dim MyQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set MyDb = CurrentDb

Set MyQueryDef = MyDb.QueryDefs( "MyQueryNam e")
For i = 0 To MyQueryDef.Fiel ds.Count - 1
...
Next
Exit Sub

ShowObject_Er r:
MsgBox Err.Description , vbCritical

End Sub

If I run a similar bit of code in an external database, depending on
which query I am referencing I get either the correct number of filds or
0 - incorrect

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim AccApp As Access.Applicat ion
Dim OtherDb As Database, MyDb As Database
Dim OtherQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set AccApp = New Access.Applicat ion
Set OtherDb = AccApp.DBEngine .OpenDatabase(D atabaseName, False,
False, Nz(PWD))

Set MyDb = CurrentDb

Set OtherQueryDef = OtherDb.QueryDe fs("MyQueryName ")
For i = 0 To OtherQueryDef.F ields.Count - 1
...
Next
Exit Sub

ShowObject_Er r:
MsgBox Err.Description , vbCritical

End Sub

The difference appears to be whether any of the fields call any code (
for example I use code to build up the names of a family from their
individual names).

Any ideas how to get round the proble. Using Access 2000
Jan 25 '07 #4
ADO solves almost everything:

Public Function GetSelectQueryC olumnCount( _
ByVal vFullPathtoDB$, _
ByVal vSelectQueryNam e$ _
)
Dim c As ADODB.Connectio n
Set c = New ADODB.Connectio n
With c
.CursorLocation = adUseClient
.Provider = "Microsoft.Jet. OLEDB.4.0"
With .Properties
.Item("Data Source") = vFullPathtoDB
End With
.Open
GetSelectQueryC olumnCount = .OpenSchema( _
adSchemaColumns , _
Array(Empty, Empty, vSelectQueryNam e)) _
.RecordCount
End With
End Function

Private Sub test()
Debug.Print GetSelectQueryC olumnCount( _
"Northwind.mdb" , _
"Order Details Extended" _
)
End Sub

Of course, this assumes that Northwind.mdb is unsecured. If it has a
password or whatever one will have to modify the connection paroperties
to accommodate such.

Jan 25 '07 #5
Thanks Lyle.

I am not familiar with ADO ( Not even sure which library reference to use),
so I will go along with Allen Brown's solution. Actually I am trying to
extract the field names to make a table in MyDb to match against Outlook
fields.

Again many thanks for your help

Phil

"Lyle Fairfield" <ly***********@ aim.comwrote in message
news:11******** *************@m 58g2000cwm.goog legroups.com...
ADO solves almost everything:

Public Function GetSelectQueryC olumnCount( _
ByVal vFullPathtoDB$, _
ByVal vSelectQueryNam e$ _
)
Dim c As ADODB.Connectio n
Set c = New ADODB.Connectio n
With c
.CursorLocation = adUseClient
.Provider = "Microsoft.Jet. OLEDB.4.0"
With .Properties
.Item("Data Source") = vFullPathtoDB
End With
.Open
GetSelectQueryC olumnCount = .OpenSchema( _
adSchemaColumns , _
Array(Empty, Empty, vSelectQueryNam e)) _
.RecordCount
End With
End Function

Private Sub test()
Debug.Print GetSelectQueryC olumnCount( _
"Northwind.mdb" , _
"Order Details Extended" _
)
End Sub

Of course, this assumes that Northwind.mdb is unsecured. If it has a
password or whatever one will have to modify the connection paroperties
to accommodate such.

Jan 25 '07 #6
On Jan 25, 1:25 pm, "Phil Stanton" <p...@stantonfa mily.co.ukwrote :
Thanks Lyle.

I am not familiar with ADO ( Not even sure which library reference to use),
so I will go along with Allen Brown's solution. Actually I am trying to
extract the field names to make a table in MyDb to match against Outlook
fields.

Again many thanks for your help

Phil

"Lyle Fairfield" <lylefairfi...@ aim.comwrote in messagenews:11* *************** *****@m58g2000c wm.googlegroups .com...
ADO solves almost everything:
Public Function GetSelectQueryC olumnCount( _
ByVal vFullPathtoDB$, _
ByVal vSelectQueryNam e$ _
)
Dim c As ADODB.Connectio n
Set c = New ADODB.Connectio n
With c
.CursorLocation = adUseClient
.Provider = "Microsoft.Jet. OLEDB.4.0"
With .Properties
.Item("Data Source") = vFullPathtoDB
End With
.Open
GetSelectQueryC olumnCount = .OpenSchema( _
adSchemaColumns , _
Array(Empty, Empty, vSelectQueryNam e)) _
.RecordCount
End With
End Function
Private Sub test()
Debug.Print GetSelectQueryC olumnCount( _
"Northwind.mdb" , _
"Order Details Extended" _
)
End Sub
Of course, this assumes that Northwind.mdb is unsecured. If it has a
password or whatever one will have to modify the connection paroperties
to accommodate such.- Hide quoted text -- Show quoted text -
Jan 25 '07 #7
Thanks Allen. It sort of works, but there are odd commas within the fields
eg FirstName & ", " & LastName AS FullName.
I will play around as I should be able to check that the preceeding letter
is a letter, not a symbol ie ASC(Preeceeding letter) 48 and
ASC(Preeceeding letter) < 123

I will play.

Thanks for your help

Phil

"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:45******** *************** @per-qv1-newsreader-01.iinet.net.au ...
Hmm. Yes, I'd forgotten it did that.

If Access can't give the count, It should be possible to parse the fields
from the SQL statement. The example below is incomplete. It just parses
the field list at the commas. You would really need to remove any literals
first, so you don't get caught with things like:
SELECT [LastName] & ", " & [FirstName] AS FullName

Anyway the basic approach would be:

Function FieldCount()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSql As String
Dim varArray As Variant
Dim lngStart As Long
Dim lngEnd As Long

Set db = OpenDatabase("C :\MyPath\MyFile .mdb")
Set qdf = db.QueryDefs("Q uery1")
strSql = qdf.SQL
lngStart = InStr(strSql, "SELECT ")
If lngStart 0& Then
lngStart = lngStart + 7&
lngEnd = InStr(lngStart, strSql, "FROM ")
strSql = Left$(strSql, lngEnd)
strSql = Mid$(strSql, lngStart)
varArray = Split(strSql, ",")
If IsArray(varArra y) Then
FieldCount = UBound(varArray ) + 1&
End If
End If

Set qdf = Nothing
db.Close
Set db = Nothing
End Function
--
Allen Browne - Microsoft MVP. Perth, Western Australia
Tips for Access users - http://allenbrowne.com/tips.html
Reply to group, rather than allenbrowne at mvps dot org.

"Phil Stanton" <ph**@stantonfa mily.co.ukwrote in message
news:45******** **************@ ptn-nntp-reader02.plus.n et...
>Thanks, Allen

The problem is with the OtherDB, not MyDb

The program works fine for ordinary queries in the OtherDB, it is only if
those queries contain calls to functions that I get no fields returned.
I have tried including a reference to the OtherDB in the reference
section of VBA, but that didn't help.

Culpa mea forgot to close the DB.

Any more thoughts?

Thanks

Phil

"Allen Browne" <Al*********@Se eSig.Invalidwro te in message
news:45******* *************** *@per-qv1-newsreader-01.iinet.net.au ...
>>Replace your 4th line with:
Set MyDb = OpenDatabase("C :\MyFolder\MyFi le.mdb")

Before the exit, close what you opened:
MyDb.Close

"Phil Stanton" <ph**@stantonfa mily.co.ukwrote in message
news:45****** *************** *@ptn-nntp-reader02.plus.n et...
I am trying to count the fields in a queryDef in an external database.

If I run this in the actaal database I get Fields.count = 6 correctly

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim MyDb As Database
Dim MyQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set MyDb = CurrentDb

Set MyQueryDef = MyDb.QueryDefs( "MyQueryNam e")
For i = 0 To MyQueryDef.Fiel ds.Count - 1
...
Next
Exit Sub

ShowObject_E rr:
MsgBox Err.Description , vbCritical

End Sub

If I run a similar bit of code in an external database, depending on
which query I am referencing I get either the correct number of filds
or 0 - incorrect

Private Sub ObjectName_DblC lick(Cancel As Integer)

Dim AccApp As Access.Applicat ion
Dim OtherDb As Database, MyDb As Database
Dim OtherQueryDef As QueryDef

On Error GoTo ShowObject_Err

Set AccApp = New Access.Applicat ion
Set OtherDb = AccApp.DBEngine .OpenDatabase(D atabaseName, False,
False, Nz(PWD))

Set MyDb = CurrentDb

Set OtherQueryDef = OtherDb.QueryDe fs("MyQueryName ")
For i = 0 To OtherQueryDef.F ields.Count - 1
...
Next
Exit Sub

ShowObject_E rr:
MsgBox Err.Description , vbCritical

End Sub

The difference appears to be whether any of the fields call any code
( for example I use code to build up the names of a family from their
individual names).

Any ideas how to get round the proble. Using Access 2000

Jan 25 '07 #8
On Jan 25, 1:25 pm, "Phil Stanton" <p...@stantonfa mily.co.ukwrote :
Thanks Lyle.

I am not familiar with ADO ( Not even sure which library reference to use),
so I will go along with Allen Brown's solution. Actually I am trying to
extract the field names to make a table in MyDb to match against Outlook
fields.

Again many thanks for your help

Phil
Multiple choice:

1. Lyle is almost seventy years old.
2. Lyle is the youngest regular poster in CDMA.
3. All of the above.

Jan 25 '07 #9
"Phil Stanton" <ph**@stantonfa mily.co.ukwrote
Thanks Lyle.

I am not familiar with ADO ( Not even sure which
library reference to use),
Your Shakespeare library... "MUCH ADO ABOUT NOTHING", of course.


Jan 25 '07 #10

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

Similar topics

20
10161
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
3
2765
by: D Denholm | last post by:
I am a Access newbie... Hopefully somebody can help me figure this out. I have a database that looks like: Asset Economic Minimum ----- ---------------- 10555 320 10555 320 10555 320
1
1579
by: Gee | last post by:
Hi Folks, I have a small database that contains test dates. We can only sit 9 people per test. I need to count the dates that are the same and NOT go over 9. I read MSAcess help on the DCount function and on the Count function, but I am still lost. It seems DCount will only work with VBA, and Count is supposed to work in a macro. I don't know VBA, so I can't use it for this purpose. As always, this
1
5420
by: Scott269 | last post by:
So I've got an old MS Works database I imported into Access. I needed a primary key so I created a record number field that was just the record number I manually inserted when I entered it in the database so I could sort the records by the date at which they were entered. Well now I've deleted some of those records so its of course causing gaps in the records. The record number in Access no longer matches my record number that I...
4
1611
by: Deborah V. Gardner | last post by:
I have a multi-user database in Access 2000. The back end is on the server and the front end is on each machine. The user will go to a field (defined as data type Memo) and make some changes. Then he will go to another field and the first field is wiped out; nothing is in it. In the OnExit command, I have DoCmd.RunCommand acCmdSaveRecord. Also, they are using an .mde file as the front end. Does anyone have any idea what is causing...
1
2938
by: Phoenix_ver10 | last post by:
I have a mailing list with multiple names going to the same addresses. I need one address with all the names for that address on it. I checked out the example on microsoft's site, but A: It doesn't work (error that there is an extra parenthise (sp?) ) and B: Will only let in two names for each record. If there are three, the middle on is deleted. Or to make things simpler, if nothing else, I'd like to add a field in the table that shows...
17
4032
by: The Frog | last post by:
Hello everyone, I am working on an application that can build database objects in MS Access from text files. I suppose you could call it a backup and restore type routine. Accessing the fields, tables, relationships, and indexes is no issue for me via DAO code. The issue I have is that I am not sure which properties are actually necessary / available to set from code for each possible type of field. I have looked fo´r a reference on...
0
1694
by: thefredzx | last post by:
i have a problem for aplication of apriori algorithm in datamining..my problem is how to make an additional field in database? for example: I have some data like shown below: ------------------------------------- id_session | id_hyperlink| ------------------------------------ 1 | 1 1 | 2 1 | 3 1 | 4 1 | 5
1
1375
by: phpnewbie112 | last post by:
Hi everyone, I am new to php and I was trying hard to alter an existing script ut I didn't succeed to... I have 2 databases: - Categories (catid, shortname, description) - Transactions (date, amount, balance, user) in the categories I have 9 categories. the script displays the categories as checkboxes to choose from. I want to add the following functionality:
0
9617
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8931
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.