【发布时间】:2014-10-20 15:40:17
【问题描述】:
我有一个数据库,其中包含水井的位置以及与这些水井相关的大量属性。据我所知,所有表都与 WELL_ID(井名)链接,这是“短文本”数据类型。在数据库中存在我试图从中获取数据的现有查询(我不想弄乱表格,以防我犯了错误并搞砸了)。
我创建了一个表单,用户在其中输入东向和北向的 UTM 坐标以及搜索半径,然后单击“搜索”按钮。单击搜索后,该过程会创建 [qryUTM_NAD83] 的记录集,然后计算每个井的径向距离,如果它在指定的搜索半径内,则使用 INSERT INTO 将其存储在新的 [Search_Results] 表中。
现在,一旦井被识别为满足搜索条件,WELL_ID 就会被存储,并传递给一个函数,该函数通过不同查询 [qryFormation] 的记录集进行搜索。此查询具有一对多的关系,其中每个地质层都有一个记录,每个地质层都有相同的 WELL_ID(即,每口井都有多个层,但所有这些层都具有相同的 WELL_ID)。我需要将这些图层连接成一个字符串,将它们传递回搜索函数并将其添加到 [Search_Results] 表中。但是,我在 SQL 语句中遇到数据类型不匹配错误。
这是我的代码: (为了方便大家,我省略了部分代码)
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
功能: (“Set rs2...”中出现错误——数据类型不匹配)
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
【问题讨论】: