Anyone know is it possible to write a macro that can create new column in sql server and after created the newly column, insert the data into the new column?
this is how i use to insert. advice me how to change it or make it to ALTER TABLE Statement from
Code:
strSQL = "Insert INTO [odbc;Driver={SQL Server};" & _
"Server=D1406;Database=OP;" & _
"UID=coc;PWD=pd1].TestDB " & _
"Select * FROM [Insert$]"
TO
Code:
ALTER TABLE Persons
ADD [2009-10-Wk1] date
ALTER TABLE Persons
ADD [2009-10-Wk2] date
ALTER TABLE Persons
ADD [2009-10-Wk3] date
ALTER TABLE Persons
ADD [2009-10-Wk4] date
ALTER TABLE Persons
ADD [2009-10-Wk5] date
and
after creating columns how can i make it insert to my new column like in my screenshot. i already square out the location.
Private Sub Command1_Click()
Dim conn As ADODB.Connection, rs As ADODB.Recordset
Dim sqlStatement As String
'~~> In case there is error
On Error GoTo ErrHandler:
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'~~> Connection String
conn.ConnectionString = _
"DRIVER={SQL Server};" _
& "SERVER=D1406;" _
& "DATABASE=OP;" _
& "UID=coc;" _
& "PWD=pd1;"
conn.CursorLocation = adUseClient
conn.Open
'~~> Add the column to the table "MyTableName" Amend as required
sqlStatement = "ALTER TABLE MyTableName ADD [2009-10-Wk1] float(8)"
Call conn.Execute(sqlStatement)
'~~> Close the recordset and connection
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
ErrHandler:
'~~> Error Handling
MsgBox Err.Number & ": " & Err.Description
End Sub
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
I presume it was on rs.Close , because the recordset is never opened. If so, that line should be removed (and anything else that refers to rs can be too).
and the data type in my SQL server is 'real' instead of float.
That is fine - they are just different names for the same thing.
i using the nomarl insert statment i got ther result in 'fig 1' but i would like to make it like my figure 3, how can i make the insert statment into my figure 3 ?
Ah.. I should have pointed out that to find out which line caused the error you need to disable the error hander, by adding ' at the start of the On Error line.
However, as the fix was valid - it means my suspicion was correct.
i using the nomarl insert statment i got ther result in 'fig 1' but i would like to make it like my figure 3, how can i make the insert statment into my figure 3 ?
Don't use Insert, because that explicitly adds a new record.
Rather than post something almost meaningless like "not working" and make us guess what it means in this particular case (which takes time, and can be wrong), actually tell us what is happening. eg:
is there an error? if so what was it? and which line was it on?