2016-04-06 2 views
0

i utilisé ce funtion pour concaténer datarows ..Requête d'appel de ms accès par vb.net

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 

la requête i est utilisé

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; 

Maintenant je dois appeler cette requête de vb. net i essayé comme suit ::

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() 

cela donne erreur comme suit « Undefine d la fonction 'GetList' dans l'expression. "

Aidez-nous! Merci!

Répondre

0

Le message d'erreur est clair. Vous envoyez la chaîne SQL à Access qui recherche alors la fonction GetList qu'elle ne trouve pas, bien sûr.

Vous devrez repenser votre concept.

+0

Merci @gustav mais j'ai besoin d'exécuter cette requête sur l'événement de clic de bouton en utilisant vb.net –

+0

Pouvez-vous me donner des suggestions .. –

+0

Oui, comme les autres ref. par Gordon dit aussi, vous devez simplement utiliser une autre méthode. – Gustav