VBA Set field data type to number during macro

jkeyes

Active Member
Joined
Apr 15, 2003
Messages
343
In the following code, during the For/Next loop, I'd like the Fields(2) data when entered to be entered as a number, rather than text which seems to be the default. I'm sure it's simple, I'm just brand new to Access VBA... thanks!

Code:
Sub AddToFlat()
Dim db As DAO.Database
Dim fldWide As DAO.Field
Dim fldFlat As DAO.Field
Dim rstWide As DAO.Recordset
Dim rstFlat As DAO.Recordset
Dim I As Long

    Set db = CurrentDb

    Set rstWide = db.OpenRecordset("Wide")
    Set rstFlat = db.OpenRecordset("Flat")

    rstWide.MoveFirst
   
    While Not (rstWide.EOF)
        With rstFlat
            For I = 1 To rstWide.Fields.Count - 1
                .AddNew
                .Fields(0) = rstWide.Fields(0)
                .Fields(1) = rstWide.Fields(I).Name
                .Fields(2) = rstWide.Fields(I)
                .Update
            Next I
        End With
        rstWide.MoveNext
    Wend
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
jkeyes,

Maybe I'm not understanding the question because I thought you already figured this one out. Your code doesn't create the table, so you have to create it ahead of time. When you create the table you can specify the data types (and size) of each field.

hth,
Giacomo
 
Upvote 0
jkeyes

Like giacomo says that code doesn't create a table.

PS Did you not see my comment regarding using a Text field to be 'safer'?

There are various different types of number field but a field can only have one data type.
 
Upvote 0
Thanks... sorry, I was running this again and saw a "Data Type Conversion" error, and didn't think through what you two had told me... I found the problem. So, nevermind, and thanks anyway!
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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