Trouble defining a field

The_Kurgan

Active Member
Joined
Jan 10, 2006
Messages
270
Hi all!

I am attempting to create a table based on a selection that the user chooses. Available to the user is the list of fields from another Access table. The new table will basically contain that chosen field and the associated type (Text, Date, or Double).

Here is my code:
Code:
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim myTable As DAO.TableDef
Dim myField As DAO.Field
Dim myProp As DAO.Property

'variables
fld = Me.List7.Value
Op = Me.List9.Value
FldAndOp = fld & " " & Op

'Get field type of field chosen by user (found in My_Data table)
Set rs = db.OpenRecordset("My_Data")
My_My_Data_Tbl = TypeName(rs.Fields(fld).Value)

'Create table with field name + operator and give the field the proper field type
With myTable
    If My_My_Data_Tbl = "Double" Then
        .Fields.Append .CreateField(FldAndOp, dbDouble)
    ElseIf My_My_Data_Tbl = "Date" Then
        .Fields.Append .CreateField(FldAndOp, dbDate)
    ElseIf My_My_Data_Tbl = "String" Then
        .Fields.Append .CreateField(FldAndOp, dbText)
    End If
End With
db.TableDefs.Append myTable   '!!!!!!!!!!!!!!!!!!!!!!!! THIS LINE GIVES ME THE ERROR !!!!!!!!!!!!!!!!!!!!!!!!
Set myField = myTable.Fields(FldAndOp)



The error I'm getting is "No field defined--cannot append TableDef or Index." The strange thing is that it seems to be happening on one date field. All other fields seem to work (including other date fields). I've double-checked the DataType in the table itself and it's the same as other date fields.

I hope this makes sense. I'd love to see what y'all come up with. TIA!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
This might not be of much help since I have limited experience with creating tables this way.
You're supposed to create the field first, then append it to the fields collection of the table def, then add the table def to the tables collection. You seem to be trying to append then create.
.Fields.Append .CreateField(FldAndOp, dbDouble) as opposed to
.CreateField(FldAndOp, dbDouble)
.Fields.Append
However, since you said it works most of the time, then have you stepped through this code to ensure at least one If statement is true within your With block? If there is a failure just one time, your code is trying to append a table def when no field was created. Are you certain that there is always a recordset returned after every user choice?
Maybe add another Else line with debug.print "My_My_Data_Tbl= " & My_My_Data_Tbl. If it's a different type, null or empty, you'll get a printout.
 
Upvote 0
I'd be willing to test (try the same thing) but I'd want to know what the values are for the variables.
 
Upvote 0
Micron: Yes, I stepped through it and everything appears fine. However, a null was being returned as the field type for that one particular field.
xenou: That is very kind of you. Thank you.

Thank you both for your replies! I stayed up last night and figured a way around it by just creating a new table with those field names in one column and the data type in the other. I'll just have to remember to add fields/types if new fields are brought into the original. It's not ideal, but it works.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,850
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top