Runtime error retrieving data from MS Access Database

dan_pafc

Board Regular
Joined
Aug 16, 2007
Messages
162
Ive posted this in the Excel forum but after thought it would probably best sit here...

Hi

Im using the following to retrieve data from MS Access Database.

The Code pulls the data fine until I add in the line "WHERE PlayerData.[MatchID] = """ & Sheet3.Range("F1").Value & """"

Where I get a 'Data Type Mismatch in Criteria Expression." run time error. Whilst im guessing its because im making use of a variable (sheet3.range("F1").Value is calculated by a Vlookup) I cannot for the life of me figure out how to fix it.

Sub GetTeamData()
Dim rsData As ADODB.Recordset
Dim sConnect As String
Dim sSQL As String
Dim sPath As String

sPath = ThisWorkbook.Path
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"

'Create the connection string
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sPath & "FFL.mdb;"

'Build the SQL query
sSQL = "SELECT PlayerData.[ID], PlayerData.[MatchID], PlayerData.[SelectionNo] " & _
"FROM PlayerData " & _
"WHERE PlayerData.[MatchID] = """ & Sheet3.Range("F1").Value & """"

'Retrieve the data using ADO

Set rsData = New ADODB.Recordset
rsData.Open sSQL, sConnect, _
adOpenForwardOnly, adLockReadOnly, adCmdText
If Not rsData.EOF Then
Sheet4.Range("M2").CopyFromRecordset rsData
End If

rsData.Close
Set rsData = Nothing

End Sub

Any help anyone can give would be hugely appreciated!!
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What data type is PlayerData.[MatchID]. If it is a number then you shouldn't be adding quotes to your criteria.

Code:
[COLOR=#333333]"WHERE PlayerData.[MatchID] = " & Sheet3.Range("F1").Value[/COLOR]

In future please use code tags on posts. If you go advanced and then select your code portion and click the # symbol above the area you type in it will insert them for you.
 
Upvote 0
Great that worked a treat

Code:
"WHERE PlayerData.[MatchID] = " & Sheet3.Range("F1").Value


Also now know about 'coding' (although I couldnt see the # symbol).

Thanks for the reply.

Dan.
 
Upvote 0

Forum statistics

Threads
1,221,848
Messages
6,162,419
Members
451,765
Latest member
craigvan888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top