Solved

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

Posted on 2012-03-12
6
197 Views
Last Modified: 2012-03-18
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
0
Comment
Question by:tenriquez199
  • 4
6 Comments
 
LVL 6

Expert Comment

by:reitzen
ID: 37712605
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.
0
 
LVL 9

Expert Comment

by:chwong67
ID: 37712618
0
 
LVL 1

Author Comment

by:tenriquez199
ID: 37715813
hi reitzen

thanks your comment

have you one exapmle?

i try to do it whitout success

thanks
sorry
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 1

Author Comment

by:tenriquez199
ID: 37715837
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
0
 
LVL 1

Accepted Solution

by:
tenriquez199 earned 0 total points
ID: 37716266
hi

i solve it

Function GetSupplierId(cell As Range)
    Dim importe  As Double
    Dim IdDoc As String
    IdDoc = ActiveCell.Offset(, -7)
    Rem IdDoc = cell.Text
 
    Dim conn As New ADODB.Connection
    Dim rs As ADODB.Recordset
    conn.Open ("Provider=sqloledb;Data Source=PCAENRIQUEZ\SQLEXPRESS;Initial Catalog=JDE;Integrated Security=SSPI")
    Set rs = conn.Execute("select importe from Facturacion where idDocumento = '" & Replace(IdDoc, "'", "''") & "'")
    GetSupplierId = rs.Fields(0).Value
 
    rs.Close
    conn.Close
    Set conn = Nothing
    Set rs = Nothing
End Function
0
 
LVL 1

Author Closing Comment

by:tenriquez199
ID: 37734380
thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now