Tuesday, December 11, 2018

MS Access insert data in SQL Server

MS Access insert data in SQL Server 



Option Compare Database

Private Sub Command27_Click()

Call GetDataFromADO

End Sub

Sub GetDataFromADO()
    'Declare variables'
        Set objMyconn = New ADODB.Connection
        Set objMyCmd = New ADODB.Command
        Set objMyRecordset = New ADODB.Recordset
        Dim rc As Long
 'Open Connection'
        objMyconn.ConnectionString = "Provider=SQLOLEDB;Data Source=LAPTOP-0ACTJCP6\SQLEXPRESS;Initial Catalog=sinkydb; Integrated Security=SSPI;"
        objMyconn.Open
        Dim strsql
        strsql = "insert into Tblinserdata(Fname,  Lname, address) select '" & tbfname.Value & "','" & tbLname.Value & "','" & TbAddress.Value & "'"
        Debug.Print strsql
         objMyconn.Execute (strsql)
    'Set and Excecute SQL Command'
        objMyconn.Close
End Sub


Wednesday, December 5, 2018

insert Table range to SQL server Database

Dim rng as Range
Dim defaultDate As string
Dim sql as string, bulkSql as string

Set rng = Range("A1:XX") -- Range of the table. 
defaultDate = Format(Range("A2").Value, "yyyy/mm/dd") 
bulkSql = ""  

'generated sample: insert tRate(ID, Rate, Date) SELECT s.ObjectId, '0.15', '2015/08/24' FROM tSecurity s where s.Number = '007'

For Each row In rng.Rows

    sql= "insert tRate(ID, Rate, Date) SELECT s.ObjectId " & "','" & row.Cells(2).Value & "','" & defaultDate & "' FROM tSecurity s where s.number = '" & row.Cells(1).Value & "'; "

    bulkSql = bulkSql & sql  

Next row

adoCn.Execute bulkSql, , adCmdText

https://stackoverflow.com/questions/32184673/inserting-values-into-sql-server-table-via-vba