Perfectly good function does not work ?

S

steve

I downloaded the following code some time ago and I belive I used it.
Im with a new company and want to use this code again.

However it does not seem to work. When I press F8 in the VB builder I
get nothing. It also does NOT show that there are any errors. It just
sits there. It does NOT high lite the function name and step through
the procedure. I have copy and pased several bits of code that seem to
give no errors, but also does nothing, just sits there. No errors no
red letters. Its like it is perfectly writen code, that does nothing.

Can anyone tell me what is wrong ?

Thanks.

Here is the code.

Function fTableExists1(strTableName As String) As Boolean
Dim db As Database
Dim tdf As Tabledef
Set db = DBEngine(0) (0)
fTableExists1 = False
For Each tdf In db.TableDefs
If tdf.Name = strTableName Then fTableExists = True
Next tdf
Set db = Nothing
End Function
 
R

Roger Carlson

If you are using Access 2000 or 2002 (XP), you need to add a Reference to
"Microsoft DAO 3.6 Object Library". You can do this in any code window by
going to Tools>References. (Access 97 and 2003 already have this
reference.)

Then you should change the code as follows:

Dim db As DAO.Database
Dim tdf As DAO.Tabledef


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
V

Van T. Dinh

F8 only works for Sub, not a function, especially the one you posted since
the function requires a String value.

Use the Immediate Window to test the function by typing:

?fTableExists1("SomeRealOrImaginaryTableName")

(including the question mark ?). VBA should return either -1 (True) or 0
(False)

BTW, there is a typing error in your code: you used both "fTableExists1" and
"fTableExists" without the 1 at the end in your code!

Check Access VB Help on "Option Explicit" that helps in detecting these
typing errors.
 
S

steve

Thank you for your response.

Im confused by a few things.
One user advised me to add MS DAO 3.6 Object Library. I did this which
I think added the functionality. Someone also informed me of a typo the
fTableExists1 was missing the 1 near the bottom. Yes.
But my question is this

When I do what youa have suggested it works, sort of. eg.
I created a table called something1 .

When I put
debug.print x = fTableExists1("somthing1")
True

AND When I
debug.print x = fTableExists1("somthing")
False

BUT
debug.print x = fTableExists1("somthin")
True

OR
debug.print x = fTableExists1("x")
True

Why am I getting the wrong output


Regards.
 
S

steve

Thank you the Library thing certainly moves me closer. I thought there
might be a lib missing I just didnt know which of the 100 or so that
are listed.
Regards
 
S

steve

Thank you. Good eye on the missing 1.

Im no programmer (obviously) but I have writen these functions before
and used the f8 key, and they work.
How do I know which one's work or dont and how am I suppose to test?

regards
 
D

Dirk Goldgar

steve said:
Thank you. Good eye on the missing 1.

Im no programmer (obviously) but I have writen these functions before
and used the f8 key, and they work.
How do I know which one's work or dont and how am I suppose to test?

You cannot -- and never could -- step into a function that requires an
argument.
 
R

Rick Brandt

steve said:
Thank you. Good eye on the missing 1.

Im no programmer (obviously) but I have writen these functions before
and used the f8 key, and they work.
How do I know which one's work or dont and how am I suppose to test?

If the first line is "Sub..." then it works. If the first line is
"Function..." then it doesn't.
 
D

Dirk Goldgar

Rick Brandt said:
If the first line is "Sub..." then it works. If the first line is
"Function..." then it doesn't.

Sorry, no. It's not about whether it's a Sub or a Function; it's about
whether it has arguments or not.
 
M

MPAPoster

steve said:
Here is the code.

Function fTableExists1(strTableName As String) As Boolean
Dim db As Database
Dim tdf As Tabledef
Set db = DBEngine(0) (0)
fTableExists1 = False
For Each tdf In db.TableDefs
If tdf.Name = strTableName Then fTableExists = True
Next tdf
Set db = Nothing
End Function

Side Note:

It would be slightly better to change the "For Each" part of your
function to:

For Each tdf In db.TableDefs
If tdf.Name = strTableName Then
fTableExists = True
Exit For
End If
Next tdf

That way once the tabledef is found it doesn't keep looking at tabledef
names.

James A. Fortune
(e-mail address removed)
 
S

steve

Dirk said:
You cannot -- and never could -- step into a function that requires an
argument.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)


You cannot -- and never could -- step into a function that requires an
argument.

Mmm thanks. The following is proof of what you are saying I guess. It
runs ok using F8, but I guess it does not require an argument.

Thanks.


Function ChangePrimaryKey()
Dim strSQL As String
'Ver 1.0
' To find duplicate you must put the primary key on the field that has
duplicates.
' to do this programically you must first remove it then add it to the
field you want
' This does it.

' remove the primary key from a table
strSQL = "ALTER TABLE tmptblStructureOnlytblLogfile DROP CONSTRAINT
PrimaryKey"
CurrentDb.Execute strSQL, dbFailOnError

' Add a new primary key to a table and field
strSQL = "ALTER TABLE [tmptblStructureOnlytblLogfile] " & _
"ADD Constraint PK_tmpTblStructureonlytblLogfile PRIMARY KEY
(HD1Label)"
CurrentDb.Execute strSQL, dbFailOnError

End Function
 
D

Dirk Goldgar

steve said:
Mmm thanks. The following is proof of what you are saying I guess. It
runs ok using F8, but I guess it does not require an argument.

Function ChangePrimaryKey()

Right. There's no argument in that function header.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top