Solved

Add and format columns in vb6

Posted on 2016-11-23
7
46 Views
Last Modified: 2016-11-23
Hi, we are fixing a code where it uses datagrid.  

We set at form load the data to display:
 sSql= "select col1, col2, col3 from TableName"
 dcRecs.ConnectionString = cnn
 dcRecs.RecordSource = sSql
 dcRecs.Refresh

Open in new window


Works fine but we need to:
- add column header to each column
- format col3 as numeric 2 decimal value
- format col2 and col3 to be displayed as center

We have tried right-click on datagrid >> properties >> Columns but it only displays 2 columns.  Also we got to increase adding the column in the designer by "Edit", but still  can't seem to get in the Column tab the DataField or in the Format tab the numeric and right-align format.

Please advice.
0
Comment
Question by:rayluvs
  • 4
  • 3
7 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 41899662
DataGrid1.ColumnHeaders = True
DataGrid1.Columns.Add 2 ' The index of columns starts at 0, so this adds the 3rd column
DataGrid1.Columns(0).Caption = "aaa"
DataGrid1.Columns(1).Caption = "bbb"
DataGrid1.Columns(2).Caption = "ccc"

DataGrid1.Columns(2).NumberFormat = "#,##0.00"
DataGrid1.Columns(0).Alignment=dbgCenter
DataGrid1.Columns(1).Alignment=dbgCenter

Open in new window

0
 

Author Comment

by:rayluvs
ID: 41899943
Worked, but we are a bit confused.

Why in design mode it displays 2 columns and when running the apps that has a script with 8 columns it displays 8 columns, not 2?

Also, we didn't add 'DataGrid1.Columns.Add 2' and it displayed all 8 columns with the format we set with your code.

Please explain.
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41899947
There must be some other process going on that adds the columns. Perhaps the DataSource adds them.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:rayluvs
ID: 41899957
Yes! Right, we have Adodc 'dcRecs' in the form (see below)
dc
 dcRecs.ConnectionString = cnn
 dcRecs.RecordSource = sSql
 dcRecs.Refresh

Open in new window


Can you explain how it ?  (because when we right-click and 'retrieve fields' it doesn't show anything in the screen design but it does shows the columns in the RUN or when executed)
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41899961
Sorry, but I've never actually used a DataGrid.
0
 

Author Comment

by:rayluvs
ID: 41899966
Oh, thought u did since helping us; strange (but  thanx for it, it helped).
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41899993
You're welcome and I'm glad I was able to help.

If you expand the “Full Biography” section of my profile you'll find links to some articles I've written that may interest you.

Marty - Microsoft MVP 2009 to 2016
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2015
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Modern/Metro styled message box and input box that directly can replace MsgBox() and InputBox()in Microsoft Access 2013 and later. Also included is a preconfigured error box to be used in error handling.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now