Solved

DBGrid control=set maxlength.

Posted on 1998-10-01
7
463 Views
Last Modified: 2013-12-25
Is there a way to set the maxlength of a field in a data grid contol?
0
Comment
Question by:RUSTY
7 Comments
 
LVL 1

Expert Comment

by:abhinavm
ID: 1496576
No there is no direct way to set maxlength of a field. but you can do it by writing some code in keypress event or beforeupdate event, which will check the length of text and if it is more than maximum length you can prompt to user or whatever you want.
0
 

Author Comment

by:RUSTY
ID: 1496577
I've been trying to write some code to do this, but cannot get it to read the len of the field in the data grid column.  Can you post a snippet of code that will do this.  Thanks.
0
 

Author Comment

by:RUSTY
ID: 1496578
Adjusted points to 20
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 4

Expert Comment

by:trkcorp
ID: 1496579
Rusty,
  True DBGrid Pro 5.0 by APEX (the upgrade to DBGrid) allows you to do this very simply.  It has a DataWidth property that you can set on a Column object.  
Ex: TDBGrid1.Columns(1).DataWidth = 255
I have used it ALOT. It prevents users from keying in data that is too long to fit in the field which as I'm sure you're aware causes an error.  I know this is probably not what you want to hear but I thought you might like to know... True DBGrid really is worth buying...
0
 
LVL 3

Accepted Solution

by:
TheAnswerMan earned 20 total points
ID: 1496580
I assume you dont have TrueGrid Pro5..
That IS a pretty badass Grid Control, Its got some cool unbound grid features too.

'Flop this Code in
Private Sub Grid1_KeyPress(KeyAscii As Integer)
 if len(Grid1) > 10 then Keyascii = 0
End Sub

'this will keep you from entering more than 10 characters per field
0
 
LVL 3

Expert Comment

by:TheAnswerMan
ID: 1496581
you could try this too.. if you know what size for each column is

Private Sub Grid1_KeyPress(KeyAscii As Integer)
 Select Case Grid1.col
 case 0
  if len(Grid1) > 1 then Keyascii = 0
 case 1
  if len(Grid1) > 2 then Keyascii = 0
 case 2
  if len(Grid1) > 3 then Keyascii = 0
 end select
End Sub

'this will allow one character in the first
2 in the second..
and three in the third
0
 

Author Comment

by:RUSTY
ID: 1496582
Ah.  Perfect.  Danke.  Aloha.  Tanx.  "A+"
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro which automatically sends attachment to Outlook 14 69
Updates not working for MS Windows 7 12 164
Best way to parse out a json string in VB6? 10 163
message box in access 4 46
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

776 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