Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Caption from Table

Posted on 2009-06-29
9
Medium Priority
?
210 Views
Last Modified: 2013-11-25
How can I develop a code wherein one of my table field will be the one who display in command.caption? Here is the scenario of my problem.. I have a table (stock) its field are            
Procode     Proname                   Tag_Name
100            Sample 1                   Spl 1
101            Sample 2                   Spl 2
103            Swan                         Swan

In my form I have a command button name cmd1 Its Index are 100,101,103
I'm trying to get the Tag_name of my Table and put it into a caption of my command button. My code below already display but the problem is.. it would only display one(1)  tag_name.
Is it possible that in this scenario, I could make the caption base on the tag_name shown above?
example cmd1(100) the caption will be "Spl 1"
              cmd2(101) the Caption will be "Spl 2" and so on...
My bases of this is the Index of my command button and the Procode of my table which is they are the same to each other. Please help me. Thanks!
'Modules
Public Function Tag_Name(ProCode) As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim sql As String
Set rs = New ADODB.Recordset
    sql = "select * from STOCK where PROCODE='" & "100" & "'"
    rs.LockType = adLockOptimistic
    rs.CursorType = adOpenKeyset
    rs.Open sql, cn
    With rs
        If .BOF = True And _
            .EOF = True Then
            Tag_Name = ""
        Else
            Tag_Name = !Tag_Name
        End If
        .Close
    End With
    Set rs = Nothing
End Function
 
Public Sub Tag1()
Dim s As Labeler
Set s = New Labeler
s.OPEN_CON "mydb", ""
                cmd1(100).Caption = s.Tag_Name("")
s.CLOSE_CON
Set s = Nothing
End Sub

Open in new window

0
Comment
Question by:Whing Dela Cruz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24739314
have you tried:
'Modules
Public Function Tag_Name(ProCode as String) As String
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim sql As String
Set rs = New ADODB.Recordset
    sql = "select * from STOCK where PROCODE='" & Procode & "'"
    rs.LockType = adLockOptimistic
    rs.CursorType = adOpenKeyset
    rs.Open sql, cn
    With rs
        If .BOF = True And _
            .EOF = True Then
            Tag_Name = ""
        Else
            Tag_Name = !Tag_Name
        End If
        .Close
    End With
    Set rs = Nothing
End Function
 
Public Sub Tag1()
Dim s As Labeler
Set s = New Labeler
s.OPEN_CON "mydb", ""
                cmd1(100).Caption = s.Tag_Name("100")
                cmd1(101).Caption = s.Tag_Name("101")
                cmd1(103).Caption = s.Tag_Name("103")
s.CLOSE_CON
Set s = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
ID: 24741381
Yes sir, When I tried it an error message says "Invalid use of Null"
 Tag_Name = !Tag_Name
0
 

Author Comment

by:Whing Dela Cruz
ID: 24741452
Sorry sir, The problem was in my database. Now the code is working already.
 Is there any other way to  shorten the code? I have 50 cmd1 sir from cmd(0) to cmd1(49). If none, its okey. if yes, Can you tell me more?
 
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24744059
>>When I tried it an error message says "Invalid use of Null"
Tag_Name = !Tag_Name

You can use:
Tag_Name = !Tag_Name  & ""

>>Is there any other way to  shorten the code? I have 50 cmd1 sir from cmd(0) to cmd1(49). If none, its okey. if yes, Can you tell me more?

Do you have anything that matches those numbers into the database? Or do you just want to set the Caption of the first 50 command buttons with the first 50 rows of the database?
0
 

Author Comment

by:Whing Dela Cruz
ID: 24744135
Yes sir, What i did is that i set the procode to be the same with the cmd1(index) . example if my index is 100 then I set also 100 as a Pocode in my database.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 24744231
what about this (not tested, from the top of my head):

Public Sub Tag1()
Dim s As Labeler
Set s = New Labeler
s.OPEN_CON "mydb", ""
dim x as command
for each x in cmd1
    x.Caption = s.Tag_Name(cmd1.Index)
next
s.CLOSE_CON
Set s = Nothing
End Sub

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
ID: 24744382
I tried sir, an error message says "Method or data member not found"
.Index
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 24744692
I made 2 mistakes. Should be:
Dim x As CommandButton
 
For Each x In cmd1
    x.Caption = s.Tag_Name(x.Index)
Next

Open in new window

0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 31598067
Its very sucessfull sir, the code is working. Once again thank you so much to you!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question