Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default ADO Connect Access


Hi all,

I'm not sure if this is the right forum to post in as my Q may b
MSAccess related. I am using an ADO connection to retieve data from a
Access database using Excel VBA - this seems to be functioning properl
(I am able to get data as required).

My problem is modifying my SQL statement - currently I have;


Code
-------------------

.Open "SELECT [Part No], [Batch Qty] FROM [" & TableName & "] " & _
" WHERE [Part No] = '01801-00408'", cn, , , adCmdText

-------------------


The above is just a test to see weather or not it worked (and it does)
I want to be able to select the data based on week numbers. The curren
[Date] is in format '24/03/2006' - how do I modify this statement?

TIA, any ideas appreciated......and again someone here may know (bob??

--
gti_jober
-----------------------------------------------------------------------
gti_jobert's Profile: http://www.excelforum.com/member.php...fo&userid=3063
View this thread: http://www.excelforum.com/showthread.php?threadid=52953

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 336
Default ADO Connect Access

You could use the VBA DatePart function: DatePart("ww", your date)

"gti_jobert" wrote:


Hi all,

I'm not sure if this is the right forum to post in as my Q may be
MSAccess related. I am using an ADO connection to retieve data from an
Access database using Excel VBA - this seems to be functioning properly
(I am able to get data as required).

My problem is modifying my SQL statement - currently I have;


Code:
--------------------

.Open "SELECT [Part No], [Batch Qty] FROM [" & TableName & "] " & _
" WHERE [Part No] = '01801-00408'", cn, , , adCmdText

--------------------


The above is just a test to see weather or not it worked (and it does).
I want to be able to select the data based on week numbers. The current
[Date] is in format '24/03/2006' - how do I modify this statement?

TIA, any ideas appreciated......and again someone here may know (bob??)


--
gti_jobert
------------------------------------------------------------------------
gti_jobert's Profile: http://www.excelforum.com/member.php...o&userid=30634
View this thread: http://www.excelforum.com/showthread...hreadid=529538


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default ADO Connect Access


I've been triyng that;


Code:
--------------------

Function VBAWeekNum(D As Date, FW As Integer) As Integer
VBAWeekNum = CInt(Format(D, "ww", FW))
End Function

--------------------



Code:
--------------------

.Open "SELECT [Part No], VBAWeekNum([Date], 1) AS [Week] FROM [" & TableName & "] " & _
" WHERE [Part No] = '01801-00408'", cn, , , adCmdText

--------------------


The code comes up with an error, the coding is wrong - anyone know the
syntax to add a VBA function into an SQL Access Statement?


--
gti_jobert
------------------------------------------------------------------------
gti_jobert's Profile: http://www.excelforum.com/member.php...o&userid=30634
View this thread: http://www.excelforum.com/showthread...hreadid=529538

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default ADO Connect Access


This is the entire Function;


Code
-------------------

Sub ADOImportFromAccessTable(DBFullName As String, _
TableName As String, TargetRange As Range)
' Example: ADOImportFromAccessTable "C:\FolderName\DataBaseName.mdb", _
"TableName", Range("C1")
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
'SQL select data
*.Open "SELECT [Part No], " & VBAWeekNum( & "[Date]" &, 1) & " As [Week] FROM [" & TableName & "] " & _
" WHERE [Part No] = '01801-00408'", cn, , , adCmdText*
For intColIndex = 0 To rs.Fields.count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
TargetRange.Offset(0, 0).CopyFromRecordset rs ' the recordset data

End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

-------------------

--
gti_jober
-----------------------------------------------------------------------
gti_jobert's Profile: http://www.excelforum.com/member.php...fo&userid=3063
View this thread: http://www.excelforum.com/showthread.php?threadid=52953

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default ADO Connect Access

Hi gti_jobert

My tip is like this
DATEPART(ww, your_date_field) AS WEEKNUMBER

Good luck

"gti_jobert" wrote:


This is the entire Function;


Code:
--------------------

Sub ADOImportFromAccessTable(DBFullName As String, _
TableName As String, TargetRange As Range)
' Example: ADOImportFromAccessTable "C:\FolderName\DataBaseName.mdb", _
"TableName", Range("C1")
Dim cn As ADODB.Connection, rs As ADODB.Recordset, intColIndex As Integer
Set TargetRange = TargetRange.Cells(1, 1)
' open the database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
DBFullName & ";"
Set rs = New ADODB.Recordset
With rs
'SQL select data
*.Open "SELECT [Part No], " & VBAWeekNum( & "[Date]" &, 1) & " As [Week] FROM [" & TableName & "] " & _
" WHERE [Part No] = '01801-00408'", cn, , , adCmdText*
For intColIndex = 0 To rs.Fields.count - 1 ' the field names
TargetRange.Offset(0, intColIndex).Value = rs.Fields(intColIndex).Name
Next
TargetRange.Offset(0, 0).CopyFromRecordset rs ' the recordset data

End With
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub

--------------------


--
gti_jobert
------------------------------------------------------------------------
gti_jobert's Profile: http://www.excelforum.com/member.php...o&userid=30634
View this thread: http://www.excelforum.com/showthread...hreadid=529538




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 400
Default ADO Connect Access

The problem is this line:
"SELECT [Part No], " & VBAWeekNum( & "[Date]" &, 1) & " As [Week] ... etc

The SQL statement will be executed by the driver but it contains VBAWeekNum
which is a UDF and therefore inaccessible by the driver.

Try:

"SELECT [Part No], DATEPART('WW',[Date]) As Week ... etc

DATEPART is part of the driver'w dialect.
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
connect excel form to access table sam Excel Discussion (Misc queries) 2 June 30th 09 05:24 PM
How do I connect to access 2007 data from excel? Marilyn Myers Excel Discussion (Misc queries) 3 December 12th 08 04:15 PM
Connect to Access Database On Remote Server UT Excel Discussion (Misc queries) 0 August 21st 07 10:42 PM
Data Entry Form Connect With Access Anna Excel Discussion (Misc queries) 0 November 24th 06 04:01 PM
connect to access database and run a query in excel VBA Bob Excel Discussion (Misc queries) 0 April 25th 06 08:12 PM


All times are GMT +1. The time now is 04:23 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"