Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access VBA
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 22nd, 2006, 07:43 AM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 159
Thanks: 0
Thanked 0 Times in 0 Posts
Default lookup value in tabel and store in continuous form

hi

mmcdonal, this one is for you. you gave me the following code for the lookup in my database to lookup the value in a table.

Private Sub Naam_afdeling_AfterUpdate()

'Takes the value from the combo box

Dim sAfdeling As String
sAfdeling = Me.Naam_afdeling

'Starts a connection to the tblAfdelingenOpvTT to get the telefoonnr

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sSQL As String

'Selects the record selected by the combo box from the table

sSQL = "SELECT * FROM tblAfdelingenOpvTT WHERE [Naam afdeling] = " & _
        "'" & sAfdeling & "'"

Set cn = New ADODB.Connection
With cn
    .ConnectionString = CurrentProject.Connection
    .CursorLocation = adUseClient
    .Open
End With

Set rs = New ADODB.Recordset
    rs.CursorType = adOpenKeyset
    rs.LockType = adLockOptimistic
    rs.Open sSQL, cn

'Displays the selected telefoonnr in the text box on the form.

    Me.Telefoonnummer = rs.Fields(1)


End Sub

I told you this works fine, and it still does. Now I want something like this in another form. This form has is a continious form (don't know if this is important). What I want is when the form opens, that the database looks up the phone number and store it the same way, but only if their is no value in the phone number field already... It should look it all up when it opens, so it is not dependant of the primary key... when it opens it should look it up for all the records on the form. I changed the code to the following for this form:

Private Sub Form_Open(Cancel As Integer)
    DoCmd.Maximize
    If IsNull(Me.Telefoonnummer) Then

        'Takes the value from the combo box

        Dim sAfdeling As String
        sAfdeling = Me.Naam_afdeling

        'Starts a connection to the tblAfdelingenOpvTT to get the telefoonnr

        Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim sSQL As String

        'Selects the record selected by the combo box from the table

        sSQL = "SELECT * FROM tblAfdelingenOpvTT WHERE [Naam afdeling] = " & _
              "'" & sAfdeling & "'"

        Set cn = New ADODB.Connection
        With cn
            .ConnectionString = CurrentProject.Connection
            .CursorLocation = adUseClient
            .Open
        End With

        Set rs = New ADODB.Recordset
            rs.CursorType = adOpenKeyset
            rs.LockType = adLockOptimistic
            rs.Open sSQL, cn

        'Displays the selected telefoonnr in the text box on the form.

          Me.Telefoonnummer = rs.Fields(1)

    End If
End Sub

When I try to run this by opening the form I get an error. If I go to the VBA to debug the line Me.Telefoonnummer = rs.Fields(1) creates the error. What should I change to make this work as well? Do you have any idea?

thanks

 
Old May 22nd, 2006, 09:22 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

It sounds like it is getting an empty recordset.I didn't have time to go over this over the weekend. I will take alook today. Hopefully a poster has a good solution, too.

mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
colour continuous form palmer Access 4 July 29th, 2009 03:26 AM
Continuous Form - Images nilkanth Access 9 July 29th, 2009 02:30 AM
Loop through records on Continuous Form paulcbyrum Access 1 May 13th, 2008 06:28 AM
Autocount in Tabel A that counts from Tabel B Bjorn2Bjorn Access 4 June 8th, 2007 07:54 AM
Continuous form / SetFocus help dbartelt Access 5 February 13th, 2006 03:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.