Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access 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 November 13th, 2006, 05:40 AM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Consolidating records to a single textbox

Hi Every body

I have a query that outputs students names on certain criteria
I want to collect these names in a single textbox in a form(separated by space or dash)
the number of records varies each time.

what can I do to achieve that

Thanks very much for your help...

Dr. Mohammad El-Nesr
Irrigation Systems Design Specialist
 
Old November 14th, 2006, 08:38 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Hi,

   You can do this by stacking the names in a text string. You would do it like this:

Put the code on the Form Detail On Format event
Create Connection
Create Recordset
Open Connection
Open Recordset on list of student names

If rs.Recordcount <> 0 Then
   rs.MoveFirst
   Do Until rs.EOF
     sNameList = sNameList & " - " & rs("NameField")
   rs.MoveNext
   Loop
End If

Me.Textbox = sNameList

Set your text box to Can Grow = Yes

Does that help?


mmcdonal
 
Old November 15th, 2006, 03:47 AM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear mmcdonal
Thank you for your help,
It surely seems to be helpful but unfortunately I did not understand the
first four lines in your code,

Create Connection
Create Recordset
Open Connection
Open Recordset on list of student names

I know that these lines reflect some abbreviations to some code
but how can I write it down,

can you,kindly, explain it through a Northwind example, or any possible example
thank you very much for your helps



Dr. Mohammad El-Nesr
Irrigation Systems Design Specialist
 
Old November 15th, 2006, 08:16 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Here is code that I use on the Form's On Current Event - (not the detail section's On Format event) - to do what you want:

'Declare variables
Dim sSQL, sRisk As String
Dim iControlActID, iLen As Integer
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

'Check PK, if exists, take it for SQL String
If IsNull(Me.ControlActID) Or Me.ControlActID = "" Then
    Exit Sub
    Else
    iControlActID = Me.ControlActID
'================================================= ======
'Risk text box 113
'-------------------------------------------------------
    'This is my query to get the list of items to concatenate
    sSQL = "SELECT * FROM tblrisk WHERE [ControlActID] = " & iControlActID

    'Open Connection to SQL Server
    Set cn = New ADODB.Connection
    With cn
        .ConnectionString = CurrentProject.Connection
        .CursorLocation = adUseClient
    End With
    cn.Open "Provider=SQLOLEDB;Data Source=SAW2K3DB01;Initial Catalog=icmt;UID=" & sUID & ";PWD=" & sPWD

    'Open recordset on table where I am getting the
    'string values to concatenate
    Set rs = New ADODB.Recordset
    rs.CursorType = adOpenDynamic
    rs.LockType = adLockOptimistic
    rs.Open sSQL, cn

    'Check for values. If yes, then loop through...
    If rs.RecordCount <> 0 Then
        rs.MoveFirst
        Do Until rs.EOF
            'Stack up list in one string
            sRisk = sRisk & ", " & rs("Risk")
        rs.MoveNext
        Loop
    End If
    'If the list is longer than "" then remove the ", "
    'from the beginning of the string
    If sRisk <> "" Then
        iLen = Len(sRisk) - 2
        sRisk = Right(sRisk, iLen)
    End If
    'Put the value in the text bax
    Me.Text113 = sRisk
    'Close the recordset
    rs.Close
    'reset the string length since I do this with 4 text boxes
    'on the same form
    iLen = 0
'
'

You will need to change the connection to the local database, or whereever your dbms is, and your SQL statement, of course.

Does this help? As you can see, I am using the same technique you are attempting.



mmcdonal
 
Old November 18th, 2006, 02:31 AM
Registered User
 
Join Date: Nov 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dear Mcdonald
Thank you for your detailed answer and speedy reply

One more question,
Is it necessary to declare these variables while using the code in the MS-Access VBA? Isn't the database connection is open by default?
If yes.. please why?
I thought in first impression that this code is written to VB.net or VB6 not for the direct VBA language..
Best regards, thank you for your care.
Dr. M. B. El-Nesr

Dr. Mohammad El-Nesr
Irrigation Systems Design Specialist
 
Old November 20th, 2006, 08:22 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

Hi,

 I use ADO and work with databases in SQL mostly. So many of my applications are not connected to the DBMS, like you would find in Access .mdb databases.

   If you are using Access for your tables, then you can even use DAO instead of ADO to connect. In that case, some declarations do not have to be made, as you suggest. I do not use DAO much, but if you are interested, you can open VBA, then do a search for DAO Connection, and DAO Recordset.

HTH


mmcdonal





Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Textbox Backcolor for certain records only camehere Access VBA 5 April 28th, 2008 06:28 AM
Consolidating multiple occurences in a single row ssray23 SQL Language 11 May 18th, 2007 11:00 AM
Undo/delete ' (single quote) in a textbox iniro VB.NET 2002/2003 Basics 1 April 6th, 2005 04:41 PM
Concatenate multiple records from table to single Ron Howerton SQL Language 6 August 23rd, 2004 08:49 AM
Consolidating Access VBA Forums Bob Bedell Forum and Wrox.com Feedback 2 September 5th, 2003 09:39 AM





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