Results 1 to 6 of 6

Thread: [RESOLVED] VBA SQL scalar stmt

  1. #1

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Resolved [RESOLVED] VBA SQL scalar stmt

    Whats the quickest way in vba for access to perform vb.net

    Execute Scalar where the sqlstmt will return a value

    i need to run a sqlstmt that will return a value
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VBA SQL scalar stmt

    Quote Originally Posted by Strider
    Whats the quickest way in vba for access to perform vb.net
    I completely don't understand this question. You want to write vba code that doesn't something in .NET? Or, something like .NET can do?
    Quote Originally Posted by Strider
    Execute Scalar where the sqlstmt will return a value
    What is Scalar?

  3. #3

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: VBA SQL scalar stmt

    i use this function below in vb.net to return a value from a table

    P_sqlstmt = SELECT COUNT(*) FROM tbl_People

    VB Code:
    1. Public Function ExecScalarSQLStmt(ByVal p_sqlstmt As String) As String
    2.         Dim sqlcmd As SqlCeCommand
    3.         Try
    4.             sqlcmd = New SqlCeCommand(p_sqlstmt, dbConn.getInstance().GetConnection())
    5.             sqlcmd.CommandText = p_sqlstmt
    6.             Return sqlcmd.ExecuteScalar()
    7.         Catch err As SqlCeException
    8.             Throw New Exception(err.Message)
    9.         End Try
    10.     End Function
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: VBA SQL scalar stmt

    If I remember correctly, scalar only returns one value. Not sure if Access supports it, but you could get a similar result with SELECT TOP 1 fldFoo...
    Although your SQL should only return one value anyway. You don't have to use ExecuteScalar in .Net, it's just more efficient for that language. Your SQL should only return one result anyway.

    From .Net help...
    Use the ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. This requires less code than using the ExecuteReader method, and then performing the operations necessary to generate the single value using the data returned by a SqlDataReader.

    A typical ExecuteScalar query can be formatted as in the following C# example:

    CommandText = "select count(*) as NumberOfRegions from region";
    Int count = (int) ExecuteScalar();
    Example
    [Visual Basic, C#] The following example creates a SqlCommand and then executes it using ExecuteScalar. The example is passed a string that is a Transact-SQL statement that returns an aggregate result, and a string to use to connect to the data source.

    [Visual Basic]
    Public Sub CreateMySqlCommand(myScalarQuery As String, myConnection As SqlConnection)
    Dim myCommand As New SqlCommand(myScalarQuery, myConnection)
    myCommand.Connection.Open()
    myCommand.ExecuteScalar()
    myConnection.Close()
    End Sub 'CreateMySqlCommand
    [C#]
    public void CreateMySqlCommand(string myScalarQuery, SqlConnection myConnection)
    {
    SqlCommand myCommand = new SqlCommand(myScalarQuery, myConnection);
    myCommand.Connection.Open();
    myCommand.ExecuteScalar();
    myConnection.Close();
    }
    [C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button in the upper-left corner of the page.
    Last edited by salvelinus; Jun 24th, 2005 at 12:48 PM.
    Tengo mas preguntas que contestas

  5. #5

    Thread Starter
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: VBA SQL scalar stmt

    Currently the way i get a value from a table is to
    Create a recordset
    Execute SQL stmt on recordset
    Check if it is EOF or BOF
    then get the value of the field and assign it to a variable


    surely there is a better way
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: VBA SQL scalar stmt

    I don't think so, in VBA.
    Tengo mas preguntas que contestas

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width