473,385 Members | 1,834 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,385 software developers and data experts.

How to set field format as percentage

759 512MB
Hello !
Please ! Can you teach me how to set a field format, in a table, as PERCENT ? Via VBA, of course

A FIELD in a TABLE.

Look:
I have a Make Table query. A field in this query has PERCENT format.
If I run the query, the field is shown OK, as PERCENT. But the field in the created table hasn't the same format.
Also I have a list box where it's row source is that table. And, as far as I know, I can not format ONLY ONE COLON in my list box to show PERCENT. So I need to change the field format in the table.

Any solution is OK if in the list box that field is shown as PERCENT.

Thank you again.
Jun 4 '11 #1

✓ answered by ADezii

The answer, Mihail, is by no means intuitive. I created a little Public Sub-Routine for you that will do the trick.
  1. Copy-N-Paste the following Sub-Routine to a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public Sub FormatFieldToPercent(strTableName As String, strFieldName As String)
    2. Dim db As DAO.Database
    3. Dim tdf As DAO.TableDef
    4. Dim fld As DAO.Field
    5. Dim prop As DAO.Property
    6.  
    7. Set db = CurrentDb()
    8. Set tdf = db.TableDefs(strTableName)
    9. Set fld = tdf.Fields(strFieldName)
    10. Set prop = fld.CreateProperty("Format", dbText, "Percent")
    11.  
    12. fld.Properties.Append prop
    13. End Sub
  2. To change the Format of the Field to 'Percent', pass to the Sub-Routine the Name of your newly created Table along with the Field Name.
  3. Assuming your New Table is named tblPercent, and the Field that needs to be formatted to 'Percent' is [Percentage]:
    Expand|Select|Wrap|Line Numbers
    1. Call FormatFieldToPercent("tblPercent","Percentage")
  4. If you now Open tblPercent, you will see that the [Percentage] Field is now properly formatted to 'Percent' with the Data being displayed as Percentages. This change will also persist.
  5. Any further questions, feel free to ask.

8 13576
NeoPa
32,556 Expert Mod 16PB
Do you mean Field? Or really Control?
Jun 4 '11 #2
ADezii
8,834 Expert 8TB
The Formatting of the Field ('PERCENT') in the Original Table will not persist after the Make Table Query is executed. You can either Manually or Programmatically change the Formatting of this Field in the New Table after it has been created.
Jun 5 '11 #3
Mihail
759 512MB
Thank you for reply, ADezii.

THIS is my question: How to ? How to change the formatting of this field, PROGRAMMATICALLY (from VBA), AFTER the table has been created.

I don't know why, but my first post has been concatenated with my second post (the answer to NeoPa question). "A FIELD in a TABLE." was the first row from my second post.
Jun 5 '11 #4
ADezii
8,834 Expert 8TB
The answer, Mihail, is by no means intuitive. I created a little Public Sub-Routine for you that will do the trick.
  1. Copy-N-Paste the following Sub-Routine to a Standard Code Module:
    Expand|Select|Wrap|Line Numbers
    1. Public Sub FormatFieldToPercent(strTableName As String, strFieldName As String)
    2. Dim db As DAO.Database
    3. Dim tdf As DAO.TableDef
    4. Dim fld As DAO.Field
    5. Dim prop As DAO.Property
    6.  
    7. Set db = CurrentDb()
    8. Set tdf = db.TableDefs(strTableName)
    9. Set fld = tdf.Fields(strFieldName)
    10. Set prop = fld.CreateProperty("Format", dbText, "Percent")
    11.  
    12. fld.Properties.Append prop
    13. End Sub
  2. To change the Format of the Field to 'Percent', pass to the Sub-Routine the Name of your newly created Table along with the Field Name.
  3. Assuming your New Table is named tblPercent, and the Field that needs to be formatted to 'Percent' is [Percentage]:
    Expand|Select|Wrap|Line Numbers
    1. Call FormatFieldToPercent("tblPercent","Percentage")
  4. If you now Open tblPercent, you will see that the [Percentage] Field is now properly formatted to 'Percent' with the Data being displayed as Percentages. This change will also persist.
  5. Any further questions, feel free to ask.
Jun 5 '11 #5
Mihail
759 512MB
Thank you very much, ADezii.

I am too tired now (is 23:15), but tomorrow, in the morning, I'll try to understand your code (for farther use :) ).

Thank you !!!!
Jun 5 '11 #6
ADezii
8,834 Expert 8TB
You are quite welcome, comprehending Code after a good night's sleep is always preferable.(LOL).
Jun 5 '11 #7
Mihail
759 512MB
THANK YOU , ADezii. The code work as I need.
But now :) I wish to learn more about .CreateProperty instruction. And Access help is poor.
Jun 6 '11 #8
NeoPa
32,556 Expert Mod 16PB
Creating properties in VBA is indeed quite advanced. Many properties shown in the Properties pane of Access objects are actually not set unless they are set to True. IE. To set such a property, it first needs to be added to the Properties collection (correctly). After it exists, it can be set to True or False whenever required. This means that when desiring to set such a property there must be code to set the value; code to create the property; and code to determine whether or not the property exists before deciding which code needs to be run (Trying to create a property that already exists triggers an error of course).

This is much more complicated than managing controls of course ;-)

An alternative approach is to use some SQL code that is simply called by the VBA, but that's another story (and even that involves more advanced SQL).
Jun 6 '11 #9

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

Similar topics

1
by: Henry Stockbridge | last post by:
I am doing some database documentation, and need to convert the field type and field formats in my code into terms I can convey to others (i.e. Date/Short Date, Number/Double, and so forth.) Any...
0
by: HardBap | last post by:
I have an Access database that has an input mask on a field, !\(999") "000\-0000;0;_ , that formats a phone number. When I fill a DataSet using an OldDbDataAdapter some of the records lose the...
2
by: Leon Shaw | last post by:
What is the best way (or How) to format a form textbox field so that when the user is entering data the text don't move (like it's trying to adjust itself or something) around?
9
by: David Rysdam | last post by:
I have a large amount of data that I copy in and out of Sybase very often. Now I also want to copy this data in and out of postgres. I have an existing script that creates the entire database(s)...
3
by: bobdydd | last post by:
Hi Everybody I am using VB.NET Express with SQL ServerExpress In VB6 and Access 2000 you could specify a number to display as a percentage to 2 decimal places. How can you do this with...
2
by: hunslair | last post by:
This is a really basic question. I am taking a teach yourself beginners course on Access 2003 and have run into a road block on one practice. In a select query, I have created a simple calculation...
2
by: alfamash | last post by:
I am working on a database created by someone else. The date field in the database accept dates like 01/01/2007 but stores 1/1/2007. Please how do I format this date field to accept 01/01/2007 and...
7
by: ARC | last post by:
This is taken from the "What's New in Access 2007" page. However, I've looked through all the properties of a text field memo box, and cannot find the append only option. Does anyone know how to...
0
by: Carlos | last post by:
Hi all, I just discovered that a field that contains an e-mail address in a gridview does not work as it used to. That is, I had this DataFormatString: <a href=mailto:{0}>{0}</a> This thing...
3
by: Chris Clavius | last post by:
I have exported data from access 2003 table to excel where my date field was not recognized as date. After looking closely to my table I then realized that I didn't set the field's format to any of...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.