用vb.net实现ms-access的呼叫查询
我用这个函数连接数据行用vb.net实现ms-access的呼叫查询,vb.net,ms-access,Vb.net,Ms Access,我用这个函数连接数据行 Public Function GetList(SQL As String _ , Optional ColumnDelimeter As String = ", " _ , Optional RowDelimeter As String = vbCrLf) As String 'PURPOSE: to return a combined
Public Function GetList(SQL As String _
, Optional ColumnDelimeter As String = ", " _
, Optional RowDelimeter As String = vbCrLf) As String
'PURPOSE: to return a combined string from the passed query
'ARGS:
' 1. SQL is a valid Select statement
' 2. ColumnDelimiter is the character(s) that separate each column
' 3. RowDelimiter is the character(s) that separate each row
'RETURN VAL: Concatenated list
'DESIGN NOTES:
'EXAMPLE CALL: =GetList("Select Col1,Col2 From Table1 Where Table1.Key = " & OuterTable.Key)
Const PROCNAME = "GetList"
Const adClipString = 2
Dim oConn As ADODB.Connection
Dim oRS As ADODB.Recordset
Dim sResult As String
On Error GoTo ProcErr
Set oConn = CurrentProject.Connection
Set oRS = oConn.Execute(SQL)
sResult = oRS.GetString(adClipString, -1, ColumnDelimeter, RowDelimeter)
If Right(sResult, Len(RowDelimeter)) = RowDelimeter Then
sResult = Mid$(sResult, 1, Len(sResult) - Len(RowDelimeter))
End If
GetList = sResult
oRS.Close
oConn.Close
CleanUp:
Set oRS = Nothing
Set oConn = Nothing
Exit Function
ProcErr:
' insert error handler
Resume CleanUp
End Function
我使用的查询是
SELECT OB.Operation_Type, OB.Machine_Type, OB.Attatchment, GetList("Select Operation_Name From OB As T1 Where T1.Operation_Type = """ & [ob].[Operation_Type] & """ and T1.Machine_Type = """ & [ob].[Machine_Type] & """ and T1.Attatchment = """ & [ob].[Attatchment] & """ ",""," + ") AS Expr1
FROM ob
GROUP BY ob.Operation_Type, Machine_Type, Attatchment;
现在我需要从vb.net调用这个查询
我做了如下尝试:
myConnection.Open()
Dim db As New OleDb.OleDbDataAdapter
Dim cn As New OleDb.OleDbConnection
Dim dt As New DataTable
Dim ds As New DataSet
Dim cmd As New OleDbCommand("Query", myConnection)
Try
cmd.Connection = myConnection
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "Query"
db.SelectCommand = cmd
db.Fill(dt)
Me.DataGridView1.DataSource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
myConnection.Close()
这是如下给出的错误
“表达式中未定义函数‘GetList’。”
请帮忙
谢谢大家! 错误消息很清楚。将SQL字符串发送给Access,然后Access将查找它当然找不到的函数GetList
你必须重新考虑你的概念。谢谢@gustav,但我需要使用vb.NET对按钮点击事件进行查询。你能给我一些建议吗?是的,正如Gordon的另一篇参考文献所说,你只需要使用另一种方法。