Solved

Add and format columns in vb6

Posted on 2016-11-23
7
91 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
[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
  • 4
  • 3
7 Comments
 
LVL 48

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 48

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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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 48

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 48

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…

695 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