Access: strSQL Alter Table (Field Types)

arcaidius

Board Regular
Joined
Dec 4, 2018
Messages
97
Hello,

Would greatly appreciate some help with VBA. I am using the records on form1 (from Table1) to enter the data type either Text or Double in "DataType" column. There will only be 10-30 records.

When complete, I want to click a macro button that updates Table2 data types, "Field1" with ([Table1], [DataType], [ID=1] ; Field2 with ([Table1], [DataType], [ID=2]) and so on.
I have the below code that works if the data type is in the code. Can anyone help me alter this to lookup the datatype from records in Table1? (also need something to stop when there's no more records, or a loop? The Field names will be "Field" and 1 thru whatever, so is there a way to do something
Excel Formula:
IF(RIGHT(1)=[Table1], [DataType], [ID#]) Then
??? and keep looping?

VBA Code:
Private Sub Command9_Click()
    
    Dim strSQL As String
    
    strSQL = "ALTER TABLE [Table2] ALTER COLUMN Field1 double"
    DoCmd.RunSQL strSQL

    strSQL = "ALTER TABLE [Table2] ALTER COLUMN Field2 double"
    DoCmd.RunSQL strSQL

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
If someone could just please help me get this code to recognize "Fname" as the field name to be altered?

VBA Code:
Private Sub DataType_AfterUpdate()

Dim strSQL As String

Fname = Forms!Form1.Recordset![Field]

 strSQL = "ALTER TABLE [Table2] ALTER COLUMN Fname double"
    DoCmd.RunSQL strSQL
End Sub
 
Upvote 0
This works for me. I changed field id from short text to number and it ends up as double.

VBA Code:
Sub jd()
10      On Error GoTo jd_Error
          Dim db As DAO.Database
20        Set db = CurrentDb
          Dim s As String
30        s = "ALTER TABLE tbOne Alter COLUMN id number primary key"
40     DoCmd.RunSQL s
50      On Error GoTo 0
jd_Exit:
60      Exit Sub
jd_Error:
70      MsgBox "Error " & Err.Number & " (" & Err.Description & "), line " & Erl & " in Procedure jd" _
           & "  Module  ADO_Etc "
80      GoTo jd_Exit
End Sub
 
Upvote 0
seems like this is cross posted
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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