As a first step, record a query using Ms Query. The resulting code will give you the connection data you need.
-- Andy Wiggins FCCA www.BygSoftware.com Excel, Access and VBA Consultancy -
<itsyash[ at ]gmail.com> wrote in message news:1157199047.002859.90530[ at ]p79g2000cwp.googlegroups.com...
[Quoted Text] >I need to enter data from Excel VBA into SQL. > > I am trying to use the query mentioned below: My challenge is that I am > in a wonderland when it comes to SQL and connection objects. I dont > know what I should have in "Data Source=Server\SQLServer;" > and "Initial Catalog=MyDatabase" > > I think 'data source' should be as it is and 'Initial Catalog' should > be the database name. I dont know where the authentication should be > done. I dont have a password for SQL, username is sa. > > Please guide me... > > Sub SQLServerInsert() > > > Dim objCommand As ADODB.Command > > > ''' Create the Command object. > Set objCommand = New ADODB.Command > objCommand.ActiveConnection = "Provider=SQLOLEDB;" & _ > "Data Source=Server\SQLServer;" & _ > "Initial Catalog=MyDatabase" > > > ''' Load the SQL string into the Command object. > objCommand.CommandText = "INSERT INTO MyTable" & _ > "(Field1, Field2, Field3, Field4) " & _ > "VALUES(Value1, Value2, Value3, Value4)" > > > ''' Execute the SQL statement. > objCommand.Execute > > > Set objCommand = Nothing > > > End Sub > > > Thanks in advance, > Yash >
|