Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2006, 12:45 PM
Authorized User
 
Join Date: Aug 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL possible??

Hi All,

nowadays I'm working on a blueprint to develop a customized ordering system and have following question : 'Is it possible to search for records in a sheet using the SQL language? (ADO?)' the sheet I want to Qry is inside the same workbook were all forms are located.

Hope someone have an answer for this one

Thx and greetings from Belgium

S.

 
Old December 6th, 2006, 01:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Either import into Access and run from there or have a look at QueryTables (Data -> Import External Data -> New Database Query...)

Maccas

 
Old December 6th, 2006, 01:23 PM
Authorized User
 
Join Date: Aug 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

not possible, the application should run in XLS,
is it possible to create a recordset via code from an xls range?

 
Old December 6th, 2006, 01:26 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Do QueryTables not work for you? I think you can get them to work as XL only.

 
Old December 6th, 2006, 01:29 PM
Authorized User
 
Join Date: Aug 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

never used, can you explain in a few lines?

 
Old December 7th, 2006, 06:42 PM
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

You can find this macro in 'excel 2003 VBA' from WROX

Public Sub QueryWorksheet()

  Dim RS As ADODB.Recordset
  Dim ConnString As String

  'inside the same workbook
  ConnString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & ThisWorkbook.Path & "\" & ThisWorkbook.Name & ";" & _
    "Extended Properties=Excel 8.0;"

  'another workbook
  ConnString = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=" & ThisWorkbook.Path & "\The other workbooks name.xls;" & _
    "Extended Properties=Excel 8.0;"


  Dim SQL As String

  ' Query based on the worksheet name.
  SQL = "SELECT * FROM [Sales$]"

  ' Query based on a sheet level range name.
  ' SQL = "SELECT * FROM [Sales$MyRange]"
  ' Query based on a specific range address.
  ' SQL = "SELECT * FROM [Sales$A1:E14]"
  ' Query based on a book level range name.
  ' SQL = "SELECT * FROM BookLevelName"

  Set RS = New ADODB.Recordset

  On Error GoTo Cleanup

  Call RS.Open(SQL, ConnString, _
    CursorTypeEnum.adOpenForwardOnly, LockTypeEnum.adLockReadOnly, _
    CommandTypeEnum.adCmdText)

Dim aField As ADODB.Field
Dim NextCell As Integer

With Sheet2.Range("a1")
  For Each aField In RS.Fields
    .Offset(0, NextCell).Value = aField.Name
    NextCell = NextCell + 1
  Next aField
  .Resize(1, RS.Fields.Count).Font.Bold = True
End With

  Call Sheet2.Range("a2").CopyFromRecordset(RS)

Cleanup:
  If (Err.Number <> 0) Then
    Debug.Print Err.Description
  End If

  If (RS.State = ObjectStateEnum.adStateOpen) Then
    RS.Close
  End If

Set RS = Nothing
End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
How Run .sql Script file in MS SQL Server 2000? aarkaycee SQL Server 2000 5 October 12th, 2009 05:43 AM
creating ssis packagte for sql server to sql serer Laxmikant_it ASP.NET 3.5 Professionals 0 November 26th, 2008 12:23 AM
Converting from MS SQL 2005 to Sql Epress edition saif44 SQL Language 0 February 16th, 2007 04:17 PM
Failed to copy objects from SQL server to SQL Serv monfu SQL Server 2000 4 December 4th, 2005 05:54 PM
Move SQL DB from one sql to another sql server Israr SQL Server 2000 3 January 24th, 2005 02:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.