Avatar of tenriquez199
tenriquez199
 asked on

how insert data from sql in a excel cell depending a cell excel value

hi

is it posible to get a data from a sql table linked to an excel spreadsheet

i have a file named minuta, and i have a sql server with a table named facturas where there are sales data registers.

i want to put the sale import taked from a sql table (dbo.facturacion) , where a field named Numdoc is equal to cell value a2

is it posible it?

thanks a lot
Microsoft ExcelMicrosoft SQL Server

Avatar of undefined
Last Comment
tenriquez199

8/22/2022 - Mon
reitzen

Yes, this can absolutely be done.  You can do this two ways:
1.  Include the cell value in A2 in your SQL script.
2.  Loop over the "Numdoc" values in your recordset and write the other field values when you find a match.

There are pros and cons to each.

Including the "Numdoc" value from A2 in your script only allows one record to be returned.  Not the most effective if you have a lot of "Numdoc" values to look up.

Looping over the "Numdoc" values in your recordset would be more efficient with a lot of "Numdoc" values in your worksheet, but looping over the recordset multiple times is also not very efficient.
chwong67

tenriquez199

ASKER
hi reitzen

thanks your comment

have you one exapmle?

i try to do it whitout success

thanks
sorry
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
tenriquez199

ASKER
hi chwong67

thanks your comments

i see reference you give me

i have a dude

how refer or obtain only a value depending a cell value , by example in this code:
With rsPubs
      ' Assign the Connection object.
      .ActiveConnection = cnPubs
      ' Extract the required records.
      .Open "SELECT * FROM Authors"
      ' Copy the records into cell A1 on Sheet1.
      Sheet1.Range("A1").CopyFromRecordset rsPubs
      
      ' Tidy up
      .Close
End With

...................................................................

i think it will be something like this:

With rsPubs
      ' Assign the Connection object.
      .ActiveConnection = cnPubs
      ' Extract the required records.
      .Open "SELECT * FROM Authors where idAuthor = " + "$A1"
      ' Copy the records into cell A1 on Sheet1.
      Sheet1.Range("A1").CopyFromRecordset rsPubs
      
      ' Tidy up
      .Close
End With

..............................................................

you know how can i do that?


thanks a lot
ASKER CERTIFIED SOLUTION
tenriquez199

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
tenriquez199

ASKER
thanks