Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strange Type Mismatch Error

Status
Not open for further replies.

GarstonMassive

Programmer
May 5, 2006
71
GB
Hi,

I'm using A97 and getting the above error message on the following line:

Set tdf = "tblTableName" & CStr(x)

Access highlights the ampersand symbol.

X has been declared as an integer and runs from 1 to 10 in a loop.

tdf has been declared as a TableDef.

Anyone shed any light on it? Something to do with References perhaps?
 
Perhaps:
Set tdf = CurrentDb.TableDefs("tblTableName" & CStr(X))
 
Do you already have tables named?

tblTableName1
tblTableName2
....

Then

set tdf = currentdb.tabledefs("tblTableName" & CStr(x))

If you are trying to create a table def with the above name look at "currentdb.createtabledef" method.
 
That's great chaps! Thanks a lot.

Also I'm now trying to open a recordset on the table name but when I try I get a "Object invalid or no longer set" error message.

The code I'm using:

Set rsRecordset = CurrentDb.OpenRecordset(tdf.Name, dbOpenSnapshot)

TIA.
 
Need to see more of your code.

That message means that the tabledef that you defined (tdf) has either been set to Nothing, has gone out of scope or has not yet been set at the point where you are issuing this command.

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Golom as you requested:

For x = 1 To 12

Set tdf = CurrentDb.TableDefs("tblTableName" & CStr(x))
Set rsRecordset = CurrentDb.OpenRecordset(tdf, dbOpenSnapshot)

debug.print tdf

Next

The error message has changed now to "Type Mismatch" and the OpenRecordset command is highlighted.

This seems to me to be a References issue???
 
I don't see (from this code snippet at least) why you need the TableDef at all. You are getting a "Type Mismatch" because you are referencing "tdf" and not "tdf.Name" in the "OpenRecordset" call.
Code:
For x = 1 To 12

   Set rsRecordset = CurrentDb.OpenRecordset("tblTableName" & CStr(x), dbOpenSnapshot)

Next
At the end of the For loop however, you will have a recordset that contains data from "tblTableName12" ... the others having been replaced as you proceed through the loop.

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Golom I changed it to "tdf.Name" and I got my original error "Object invalid or no longer set".

As I loop through the tables I do some data manipulation but I haven't included it here as it's not important.

Any ideas?
 
Do you possibly have an On Error Resume Next somewhere before this?

It is possible that the

Set tdf = CurrentDb.TableDefs("tblTableName" & CStr(x))

Is raising an error and "tdf" is not being set.
Try referencing tdf before you use it in the OpenRecordset as in
Code:
Set tdf = CurrentDb.TableDefs("tblTableName" & CStr(x))
Debug.Print tdf.Name

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
No there's no Error handling in there at all.

This is very strange and leads me to think it's something to do with refs.

If I try your code above it says the same error message. When I hover over the "tdf.Name" syntax the message pops up in a tooltip.
 
...fixed it. I changed "CurrentDB" to "DBEngine(0)(0)" and it worked.

Think it maybe a A97 thing or something to do with the fact that DAO 2.5 is referenced.

Dont go there - I know.......

Thanks for your assistance fellas. This forum is great.
 
Now we're getting somewhere. Try this
Code:
Set tdf = CurrentDb.TableDefs("tblTableName" & CStr(x))
 
MsgBox "tblTableName" & CStr(x) & vbCrLf & _
       TypeName(tdf) & vbCrLf & _
       Err.Number & " - " & Err.Description
It may be that you have turned off error messages in Access. It is clear however that the "Set" statement is failing and that may be due to "tdf" not being the correct object type or to the fact that "tblTableName" & CStr(x) is a non-existent table name.



[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top