473,387 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Programming vba label/text box

Hi, please can anyone help me out with this.
I have been struggling with it for days and no result yet.
I am trying to run SQL queries that retrieve Tables stored in SQL server and then place the table in a VBA FORM label or textbox.
So far there has not been any success as this is only returning back the query I give in on my FORM label.
I am sure the query is working because I can see the result in an excel sheet.
Please can someone tell me where I am getting it wrong.
I have also attached a screen shot of the FORM to this question.
Thanks

Expand|Select|Wrap|Line Numbers
  1. Private Sub ENTER_Click()
  2. Dim cn As Object
  3. Dim rs As New ADODB.Recordset
  4. Dim strFile As String
  5. Dim strCon As String
  6. Dim strSQL, strInput As String
  7. strCon = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=KBOW;Data Source=10.9.0\KADE;Packet Size=4096;Use Encryption for Data=False;Tag with column collation when possible=False;"
  8. Set cn = CreateObject("ADODB.Connection")
  9. cn.Open strCon
  10. If ComboBox1.ListIndex = -1 Then
  11. MsgBox "No Test Selected!", , "KBOW"
  12.  ElseIf ComboBox1.Value = "Functional Test" Then
  13. strSQL = "SELECT ModuleId,EntryDate FROM inventoryModuleLocation INNER JOIN " _
  14. & " dbo.InventoryLocationList ON dbo.InventoryLocationList.LocationCode=dbo.inventoryModuleLocation.LocationCode; "
  15. Set rs = CreateObject("ADODB.RECORDSET")
  16. rs.ActiveConnection = cn
  17. rs.Open strSQL
  18.     For iCols = 0 To rs.Fields.Count - 1
  19. Worksheets("Sheet2").Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
  20. Sheet2.Range("A2").CopyFromRecordset rs
  21. ResultLabel.Caption = strSQL
  22.     Next
  23. End sub 
  24.  
Attached Images
File Type: png dbscreen.png (24.7 KB, 90 views)
Jul 1 '15 #1
8 1776
Rabbit
12,516 Expert Mod 8TB
Line 21. You set the caption to strSQL.
Jul 1 '15 #2
Thanks for your help Rabbit.
I did not get that please.
Am I supposed to name my caption strSQL?
I just tried that but it did not work
Jul 1 '15 #3
Rabbit
12,516 Expert Mod 8TB
No. The reason the caption is showing the SQL code is because you set it to strSQL which contains the SQL code. If you want to set it to the value returned by the SQL, then you need to set it to a value from the recordset. Similar to what you did for the cells on line 19.
Jul 1 '15 #4
Thank you so much Rabbit
I just edited my code and I have that expression written as :
ResultLabel.Caption = rs.Fields(iCols).Name
But it only returned first cell value of the last columns in the database.
I would be glad if you can probably give me a sample expression in this case.
Thanks for your help
Jul 2 '15 #5
Rabbit
12,516 Expert Mod 8TB
I said similar, not exactly the same. It depends on what you actually want to show in the caption, you haven't told us exactly what should be displayed in the caption. If you want to display the whole row, you need to concatenate the result of each cell.
Jul 2 '15 #6
Thanks Rabbit for the effort.
Basically I am trying to display a table in the caption.The table is coming from an SQL server. Like I said earlier, each time I run this script I am able to see my result perfectly on excel sheet "sheet2" but the problem is writing the same result into the Label on my FORM instead of Excel.
Another Idea I got is to first write the table into an excel sheet and then read the data from excel sheet into the Label.I have not being successful with this also.
I am sorry for sounding so amateur like. This is actually my first VBA project.
Jul 6 '15 #7
I added this line to my code
ResultLabel.Text = rs.Fields(0).Name & rs.Fields(1).Name & rs.Fields(2).Name

After this I got the very first row on my table which are just table headers. I was not able to get the rest. Do I need a loop for this? I tried one but not working.
Jul 6 '15 #8
Rabbit
12,516 Expert Mod 8TB
That's because you're using the .Name property. That returns the name of the field, not the value. Leave that out if you want to retrieve the value. And yes, you will need a loop if you want to concatenate multiple rows together.

But why would you want to display it like this? Displaying an entire table in a caption is such a weird way to display table data when you can just use a table.
Jul 6 '15 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: BillZondlo | last post by:
How come when I put the label text setting here: /// The main entry point for the application. /// </summary> static void Main() { Application.Run(new Form1()); } private void LoadInfo()
2
by: kaczmar2 | last post by:
I have an ASP.NET page written in VB.NET that has a label: <asp:Label runat="server" ID="lblStatus" CssClass="LabelTxt"></asp:Label> In my code behind, I am running some stored procedures and...
3
by: Mike Johnson | last post by:
I'm new to VB.Net and programming. I just brought VB.Net Standard I'm working on a small program for work. I've created two forms the first is named Forms1 and the second is named SettingsForm on...
3
by: Mike Johnson | last post by:
Thanks for the quick responses. I'm having trouble understanding. I've included the code I using. perhaps someone can tell me what I'm doing wrong. My original question was, I'm new to VB.Net...
2
by: Joe Delphi | last post by:
Hi, I want to position label text so that it always appears centered when the user re-sizes the browser window. I tried adding the HTML property text-align="CENTER" to the code, but ASP.NET...
3
by: ASP Developer | last post by:
I have a variety of pages that use a master page. For some reason when I attempt to copy any of the label text on the screen it seems to highlight most of the screen randomly and I can't simply...
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
6
by: MehrnooshD | last post by:
Hello,I am new to C#. I have a UserControl. I could not Refresh a label text in the UserControl from another form. The Codes are: public partial class ImagePBX : UserControl { ...
0
by: vijeta | last post by:
Well, I came across a problem in which based on system selected language, my label text will change. This is called Localization in C#/.Net (Localization is a technique to implement local and...
4
by: hkw9105 | last post by:
Hi, I am trying to change label text on button click, the way label.text = "string" work well before but not in this case. I tried to put this on other method and it work..it is just not working...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.