1

I have a database containing locations of water wells and a ton of properties associated with those wells. All tables are linked with WELL_ID (name of the well), which is "short text" data type, from what I can tell. Within the database there are existing queries from which I'm trying to get the data (I don't want to mess with the tables in case I make a mistake and mess something up).

I've created a form where the user inputs UTM coordinates for easting and northing, as well as a search radius, then clicks a "search" button. Upon clicking search, the procedure creates a recordset of the [qryUTM_NAD83], then calculates the radial distance of each well and if it is within the specified search radius, it is stored in a new [Search_Results] table using INSERT INTO.

Now, once the well is identified as meeting search criteria the WELL_ID is stored, and passed to a function which searches through a recordset of a different query [qryFormation]. This query has a one-to-many relationship where there is a record for each geologic layer, each having the same WELL_ID (i.e. each well has multiple layers but all these layers have the same WELL_ID). I need to concatenate these layers into one string, pass them back to the search function and add it to the [Search_Results] table. However, i get a data type mismatch error in the SQL statement.

Here's the code I have: (I've omitted some parts of code to keep it short for you all)

Private Sub SearchButton_Click()

Dim WellID As String, mySQL As String, NewLitho As String

'Creating new recordsets from [qryUTM_NAD83] table
Dim rs1 As DAO.Recordset
'[ZONE] is just user specified, helps me narrow the search a little
Set rs1 = CurrentDb.OpenRecordset("SELECT [qryUTM_NAD83].* " & _
                                  "FROM [qryUTM_NAD83] " & _
                                  "WHERE [ZONE] = " & frmZone, dbOpenDynaset)
'Moving through the recordset
rs1.MoveFirst
Do While Not rs1.EOF
    'calculated radius, r , for this well (omitted)
    If r < SearchRadius Then
        WellID = Val(rs1.Fields("WELL_ID").Value)
        NewLitho = LithoTransform(WellID)
        mySQL = "INSERT INTO [Search_Results] " & _
                "([WELL_ID], [Easting], [Northing], [Radius], [Lithology]) " & _
                "VALUES (" & WellID & ", " & x & ", " & y & ", " & r & ", " & NewLitho & ")"
        CurrentDb.Execute mySQL
        rs1.MoveNext
    End If
Loop
End Sub

The function: (error occurs in "Set rs2..." - data type mismatch)

Public Function LithoTransform(CurrentID As String) As String

'CurrentID is the well which was identified as being within the search radius
Dim rs2 As DAO.Recordset
Dim mySQL2 As String

'SQL statement and new recordset of the well we are looking at in the search
Debug.Print CurrentID
mySQL2 = "SELECT [qryFormation].* " & _
         "FROM [qryFormation] " & _
         "WHERE [WELL_ID] = " & CurrentID
Set rs2 = CurrentDb.OpenRecordset(mySQL2, dbOpenDynaset)

'Move through recordset rs2 and concatenating into new string
'Bunch of code here

'Close recordset set it to 'nothing'

End Function
0

1 Answer 1

2

Since WELL_ID is text type, include quotes around the value of CurrentID when you include it in your WHERE clause. So the quickest fix is probably this ...

mySQL2 = "SELECT [qryFormation].* " & _
         "FROM [qryFormation] " & _
         "WHERE [WELL_ID] = '" & CurrentID & "'"

However, you could switch to a parameter query instead and thereby avoid issues with quotes. Here is an untested example ...

Dim db As DAO.Database
Dim qdf AS DAO.QueryDef
Dim rs2 As DAO.Recordset
Dim mySQL2 As String

mySQL2 = "SELECT [qryFormation].* " & _
         "FROM [qryFormation] " & _
         "WHERE [WELL_ID] = [which_id]"
Set db = CurrentDb
Set qdf = db.CreateQueryDef(vbNullString, mySQL2)
qdf.Parameters("which_id") = CurrentID
Set rs2 = qdf.OpenRecordset

Use the Access help system to check functions, syntax, etc. in that code in case I made errors. Since you're new to Access, it will be to your advantage to get comfortable with its help system.

1
  • Thanks a lot, I had a feeling it would be something as simple as this. I've been using the Access help, and web searches, there's just a lot of stuff to wrap my head around. I'll try making a parameter query as well. Thanks again!
    – Sashman
    Oct 20, 2014 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.