Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 2 / May 2008

Tip: Looking for answers? Try searching our database.

Very basic ADO VB code on a Form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Christoph - 22 May 2008 03:23 GMT
Hi everybody,

I hope nobody is going to laugh about this post. I am in the very
early steps of learning VBA for Access 2007. I have some basic
programming skills in VB, so that helps a bit.

I have a very basic form with two text fields, "Text1" and "Text7" and
a button "cmdTestButton". The underlying table for this form is a
table "tblBasic" that has three fields [ID], [LastName] and
[FirstName].

I can enter a first name and a last name in the two text fields and
then click the button. The code that is correctly executed to add an
entry to my table looks like this:

---

Private Sub cmdTestButton_Click()

   Dim rs As ADODB.Recordset

On Error GoTo HandleError

   Set rs = New ADODB.Recordset
   rs.Open "tblBasic", CurrentProject.Connection, adOpenDynamic,
adLockOptimistic

   With rs

   .AddNew

      ![LastName] = Me.Text1.Value
      ![FirstName] = Me.Text7.Value
      .Update

   End With

   rs.Close
   Set rs = Nothing

ExitHere:
   Exit Sub

HandleError:
   MsgBox Err.Description
   Resume ExitHere

End Sub

---

What I am completely puzzled about is how I can now run a query in
ADO. Assume I put a last name into the Text1 textbox. Then I have a
second button "cmdSecondButton". If I push this button then I'm trying
to execute code that will find all records in my table that have the
[LastName] field equal to what last name I entered in Text1. I would
like for the first recordset to be displayed in the two text boxes.
However, I'm getting "No value given for one or more parameters." when
executing this code:

---

Private Sub cmdSecondButton_Click()

   Dim rs As ADODB.Recordset
   Dim strSQL As String

On Error GoTo HandleError

   Set rs = New ADODB.Recordset

   strSQL = "SELECT tblBasic.ID, tblBasic.LastName,
tblBasic.FirstName FROM tblBasic " _
     & "WHERE [LastName] = " _
     & Me.Text1

   rs.Open strSQL, CurrentProject.Connection, adOpenDynamic,
adLockOptimistic

   With rs

       Me.Text1.Value = ![LastName]
       Me.Text7.Value = ![FirstName]

   End With

   rs.Close
   Set rs = Nothing

ExitHere:
   Exit Sub

HandleError:
   MsgBox Err.Description
   Resume ExitHere

End Sub

---

What am I doing wrong? And second, if there are more than one
recordsets that the query returns, how can I "move" to the next one
and display that one, etc. (in other words, creating my own "navigate
forward" button). In my References, I have in addition to the first
three items "Microsoft ActiveX Data Objects 2.8 Library" selected. I
tried the 6.0 but I had problems running the code. Is 2.8 the right
selection? Do I need to select anything else?

I know it's a lot but maybe somebody has the motivation to help me
out. It is greatly appreciated.

Best regards,
Christoph
Brendan Reynolds - 22 May 2008 09:44 GMT
<snip>
>    strSQL = "SELECT tblBasic.ID, tblBasic.LastName,
> tblBasic.FirstName FROM tblBasic " _
>      & "WHERE [LastName] = " _
>      & Me.Text1
<snip>

You're missing the quotes around the string parameter ...

   strSQL = "SELECT tblBasic.ID, tblBasic.LastName, tblBasic.FirstName FROM
tblBasic " _
     & "WHERE [LastName] = '" _
     & Me.Text1 & "'"

That's a single quote followed by a double quote after the "=" and a single
quote between two double quotes at the end.

You also need to allow for the possibility that the text might include
embedded quotes, e.g. O'Brien, D'Arcy ...

   strSQL = "SELECT tblBasic.ID, tblBasic.LastName, tblBasic.FirstName FROM
tblBasic " _
     & "WHERE [LastName] = '" _
     & Replace(Me.Text1, "'", "''") & "'"

The on-line help topic at the following URL might help ...

http://office.microsoft.com/client/helppreview.aspx?AssetID=HV100481361033&Query
ID=bRUD2F0f6&respos=1&rt=2&ns=MSACCESS.DEV&lcid=2057&pid=CH101004531033


Signature

Brendan Reynolds

Christoph - 22 May 2008 14:43 GMT
On May 22, 4:44 am, "Brendan Reynolds"
<brenr...@discussions.microsoft.com> wrote:
> <snip>>    strSQL = "SELECT tblBasic.ID, tblBasic.LastName,
> > tblBasic.FirstName FROM tblBasic " _
[quoted text clipped - 27 lines]
> --
> Brendan Reynolds

Thank you very much, Brendan!
Christoph
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.