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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

giacomo

Well-known Member
Joined
Feb 20, 2002
Messages
1,796
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,351
Office Version
  1. 365
Platform
  1. Windows
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.
 

jkeyes

Active Member
Joined
Apr 15, 2003
Messages
343
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!
 

Forum statistics

Threads
1,171,060
Messages
5,873,523
Members
432,982
Latest member
meepio

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
Top