Solved

Add and format columns in vb6

Posted on 2016-11-23
7
56 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

770 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