p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Access (http://p2p.wrox.com/forumdisplay.php?f=18)
-   -   using fields in recordset to populate textbox (http://p2p.wrox.com/showthread.php?t=4650)

stoneman October 7th, 2003 07:24 AM

using fields in recordset to populate textbox
 

I have a db on a company in which I want to make available
Company name, address, phone numbers tax rate.
 I build a table named tblStandard with one row only to that will hold all these information. I have these fields; Name, Address, Phone and TaxRate as just 3 examples.

I would like to write just 1 procedure using SQL statement and create 1 recordset and be able to access any field in this recordset and display them in textboxes on other forms. (I do not want to used DLookup functions).

I have created a module and write a Public Function to create the recordset.

Public Function StandardValues(ctl As Control, str As String)

    'ctl is the type of control to populate
    ' str is the name of the control


    Dim rst As ADODB.Recordset
    Dim cnn As ADODB.Connection


    Set rst = New ADODB.Recordset
    Set cnn = CurrentProject.Connection
    rst.Open "SELECT tblStandard.* FROM tblStandard", cnn, adOpenKeyset, , adCmdText ‘ selecting all fields

    If ctl.Name = str Then
        ctl = rst!TaxRate ' populate textbox name Tax on form
    End If


End Function


I would call the above function when I click an unbound checkbox and populate Tax textbox

Private Sub CheckBox_Click(acCheckBox, Tax)

    Call StandardValues
End Sub

I cannot get it to work and cannof find out what I am doing wrong. I am using Access 20000

Bob Bedell October 7th, 2003 11:02 AM

This might help you a little furter along. First, altering your click event's parameter list signature as you are won't help. The click event can't recieve arguments that way. Second, your function needs to return a value and doesn't. The following code assumes you have 1 checkbox associated with one text box (though I'm not clear that that is what you mean):

I created 1 standard module to handle your data access and select the appropriate value based on the check box selected:

Public Function StandardValues(strField As String) As String

    Dim rst As ADODB.Recordset
    Dim cnn As ADODB.Connection
    Dim intTextBoxValue As Integer

    Set rst = New ADODB.Recordset
    Set cnn = CurrentProject.Connection
    rst.Open "SELECT tblStandard.* FROM tblStandard", cnn, adOpenKeyset, , adCmdText

    Select Case strField
        Case "Name"
            StandardValues = rst(0)
        Case "Address"
            StandardValues = rst(1)
        Case "Phone"
            StandardValues = rst(2)
        Case "TaxRate"
            StandardValues = rst(3)
    End Select

    rst.Close

End Function

The function recieves a field name as an argument and returns a string value to the calling procedure by assigning the recordset field value to the function name.

The code for the check box click events looks like this(I did Name and TaxRate):

Private Sub chkNameTextBox_Click()
    Dim strValue As String
    Dim strFieldName As String

    strFieldName = "Name"

    strValue = StandardValues(strFieldName)
    txtName = strValue
End Sub

Private Sub chkTaxCheckBox_Click()
    Dim strValue As String
    Dim strFieldName As String

    strFieldName = "TaxRate"

    strValue = StandardValues(strFieldName)
    txtTax = strValue

End Sub

The event handlers pass a field name to the fucntion and recieve a value back.

HTH,

Bob



Bob Bedell October 7th, 2003 11:03 AM

Delete the line 'Dim intTextBoxValue As Integer'. It isn't doing anything.



All times are GMT -4. The time now is 02:39 AM.

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