Solved

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

Posted on 2012-03-12
6
223 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xml files 7 29
sql 2016 data tools breakdown.. 1 15
SQL Recursion schedule 13 18
MS SQL AND PASSING A TABLE NAME TO A SPROC 5 18
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

830 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