VBA query SQL Server

By the way, VBA stands for Visual Basic for Application, which is different from VB.

This script can be used in Microsoft Access 2003 for querying SQL server.

Private Sub Command2_Click()

    Dim connection As ADODB.connection

    Set connection = New ADODB.connection

    connection.ConnectionString = "Provider=SQLOLEDB; Data Source=.\SQLEXPRESS;Database=MMD_GUI;UID=user;password=pwd;"

    connection.Open

    

    Dim result As ADODB.Recordset

    Set result = connection.Execute("SELECT top 1 UserName FROM dbo.Users")   'Can execute stored procedure with parameters here too

    

    While (result.EOF = False)

        Label4.Caption = result!UserName

        result.MoveNext

    Wend

    connection.Close   

End Sub