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

Update a table's caption

5
Hi- my first post here. Kind of a newbie. I want to update a fields caption programmatically. SQL or VB is fine and the table will always have an existing caption to overwrite.

Can u help me out?
Jun 13 '09 #1
13 10092
ADezii
8,834 Expert 8TB
@Davbib
  1. I created a Generic Function that will change a Field Caption in a given Table to another Caption, simply pass the Function 3 Arguments:
    1. The Table Name (strTableName)
    2. The Original Field Caption (strOldCaption)
    3. The New Caption for the Field (strNewCaption)
    Expand|Select|Wrap|Line Numbers
    1. Public Function fChangeFieldCaption(strTableName As String, strOldCaption As String, strNewCaption As String)
    2. On Error GoTo Err_fChangeFieldCaption
    3. Dim MyDB As DAO.Database
    4. Dim fld As DAO.Field
    5.  
    6. If Len(strTableName) = 0 Or Len(strOldCaption) = 0 Or Len(strNewCaption) = 0 Then
    7.   MsgBox "The Field Caption change cannot be completed", vbExclamation
    8.   Exit Function
    9. End If
    10.  
    11. Set MyDB = CurrentDb
    12. Set fld = MyDB.TableDefs(strTableName).Fields(strOldCaption)
    13.  
    14. fld.Properties("Caption") = strNewCaption
    15.  
    16. Set MyDB = Nothing
    17. Set fld = Nothing
    18.  
    19. Exit_fChangeFieldCaption:
    20.   Exit Function
    21.  
    22. Err_fChangeFieldCaption:
    23.   MsgBox Err.Description, vbExclamation, "Error in fChangeFieldCaption()"
    24.   Resume Exit_fChangeFieldCaption
    25. End Function
    26.  
  2. To change the Caption of [Field1] in Table1 to NEW CAPTION:
    Expand|Select|Wrap|Line Numbers
    1. Call fChangeFieldName("Table1","Field1","NEW CAPTION")
  3. To remove the Caption from [Field1] in Table1:
    Expand|Select|Wrap|Line Numbers
    1. Call fChangeFieldName("Table1","Field1"," ")
P.S. - This Code will only work if a Caption for the Field previously existed, which you have indicated will always be True. Should this not be the case, then the Caption Property would have to be dynamically created, since Caption is a User Defined Property.
Jun 13 '09 #2
NeoPa
32,556 Expert Mod 16PB
If you have a field object, then from there the reference would be :
Expand|Select|Wrap|Line Numbers
  1. .Properties("Caption")
NB. This will fail if that field has no Caption property.
Jun 15 '09 #3
NeoPa
32,556 Expert Mod 16PB
With the same field, it is also possible to add or remove the optional Caption property. Changes made this way are persistent and effect the design of your table until changed.

To add a Caption property to a field you need first to create the property object, before then appending this new object to the Properties collection. Assuming you have code where the Field object has been the subject of a With statement, a single line to add your property might be (avoiding variables) :
Expand|Select|Wrap|Line Numbers
  1. Call .Properties.Append(.CreateProperty("Caption", dbText, "Caption Value"))
To remove this property simply use :
Expand|Select|Wrap|Line Numbers
  1. Call .Properties.Delete("Caption")
Jun 15 '09 #4
ADezii
8,834 Expert 8TB
Hello NePa, just some useless information. The usual approach when dealing with User Defined Properties is to specifically Trap Error #3270, as indicated in the SHIFT ByPass code below. The OP stated that there will always be a Caption Property, so I did not incorporate this into the code in order to try to avoid confusion since he/she is a Newbie:
Expand|Select|Wrap|Line Numbers
  1. Private Function ChangeProperty(strPropertyName As String, varPropertyType As Variant, varPropertyValue As Variant) As Integer
  2. On Error GoTo Err_ChangeProperty
  3. Dim MyDB As DAO.Database
  4. Dim MyProperty As DAO.Property
  5.  
  6. Set MyDB = CurrentDb()
  7.  
  8. 'Property exists, so set its Value
  9. MyDB.Properties(strPropertyName) = varPropertyValue
  10. ChangeProperty = True
  11.  
  12. Exit_ChangeProperty:
  13.   Exit Function
  14.  
  15. Err_ChangeProperty:
  16.   If Err.Number = 3270 Then       'Property not found
  17.     'Since the Property isn't found, create it!
  18.     Set MyProperty = MyDB.CreateProperty(strPropertyName, varPropertyType, varPropertyValue)
  19.     MyDB.Properties.Append MyProperty
  20.       Resume Next
  21.   Else
  22.    'Unknown Error
  23.    ChangeProperty = False
  24.      Resume Exit_ChangeProperty
  25.   End If
  26. End Function
Expand|Select|Wrap|Line Numbers
  1. Dim intFunctionReturn As Integer
  2.  
  3. 'Allow ShiftByPass
  4. intFunctionReturn = ChangeProperty("AllowBypassKey", dbBoolean, True)
Jun 15 '09 #5
NeoPa
32,556 Expert Mod 16PB
Indeed ADezii. I hadn't overlooked that (for a change). To be clear - I see no problem with your code.

I simply added some extra information so that some of the many searchers for similar info could find what they need also (Your info about trapping error #3270 is also valuable in that category).
Jun 15 '09 #6
Davbib
5
Thanks for the response. I can't seem to get this to work. I should also add that although I will always have an existing field caption, I won't know what that field caption is (doubt that matters). What I want to do is put the value captured in the first input box (q1) into a field's caption in a table.

Here is my original code for reference:
Expand|Select|Wrap|Line Numbers
  1. Sub Import()
  2.  
  3. On Error GoTo Err_Trap
  4.  
  5. DoCmd.SetWarnings "0"
  6.  
  7. Dim name, path1
  8.  
  9. path1 = "C:\documents and settings\"
  10. name = Environ("username")
  11.  
  12. q1 = InputBox("Enter the file name of the first quarter to compare (FYyyQa.txt)", "Quarter 1")
  13.  
  14. q2 = InputBox("Enter the file name of the second quarter to compare (FYyyQb.txt)", "Quarter 2")
  15.  
  16. filestrQ1 = path1 & name & "\My Documents\" & q1
  17. filestrQ2 = path1 & name & "\My Documents\" & q2
  18. filestrSD = path1 & name & "\My Documents\Sourcing Document.xls"
  19. filestrPricing = path1 & name & "\My Documents\Pricing.xls"
  20.  
  21. ck = 0
  22.  
  23. If Dir(filestrQ1) = "" Then ck = ck + 1
  24. If Dir(filestrQ2) = "" Then ck = ck + 1
  25. If Dir(filestrPricing) = "" Then ck = ck + 1
  26. If Dir(filestrSD) = "" Then ck = ck + 1
  27.  
  28. If ck <> 0 Then
  29.  
  30. MsgBox "Sorry, " & ck & " file(s) are either missing or named improperly.  Please check your 'My Documents' folder."
  31.  
  32. Else
  33.  
  34. DoCmd.Hourglass True
  35.  
  36. Clear_Tables
  37.  
  38. DoCmd.TransferText acImportDelim, "COST_1 Spec", "COST_1", q1, yes
  39. DoCmd.TransferText acImportDelim, "COST_2 Spec", "COST_2", q2, yes
  40. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "SD", filestrSD, 1
  41. DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "PRICING", filestrPricing, 1
  42.  
  43. DoCmd.OpenQuery "Cost_Savings_Forecast_1", acViewNormal ' for 291
  44. DoCmd.OpenQuery "Cost_Savings_Forecast_2", acViewNormal ' for non-291
  45.  
  46. DoCmd.Hourglass False
  47.  
  48. Beep
  49. MsgBox "Raw data successully imported"
  50.  
  51. End If
  52.  
  53. Err_Trap_Exit:
  54.     Exit Sub
  55.  
  56. Err_Trap:
  57.     MsgBox Err.Description
  58.     Resume Err_Trap_Exit
  59.  
  60. End Sub
Jun 15 '09 #7
NeoPa
32,556 Expert Mod 16PB
@Davbib
Probably won't matter in your scenario, but I included code in my earlier post that would read that for you just in case.
@Davbib
Can you clarify if this is the code you produced after reading the replies or the original as stated. Clearly there's very little point in your posting the original code.
Jun 15 '09 #8
Davbib
5
Sorry. Thought that maybe I was making it harder than it needed to be. What I did was copy the code provided and then tested it this way.
Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2.  
  3. Call fChangeFieldName("Cost_Savings_Forecast_Table", "FC Qty", "Test Caption Change")
  4.  
  5. End Sub
when I run this, I get "Compile Error- Sub or Function not defined.
Jun 15 '09 #9
ADezii
8,834 Expert 8TB
@Davbib
fChangeFieldName() must be Declared as a 'Public' Function in a 'Standard Code Module'.
Jun 15 '09 #10
NeoPa
32,556 Expert Mod 16PB
@Davbib
Where did you put the pasted copy of fChangeFieldCaption()?

As ADezii says it must be available either in a standard code module or in the same module as the test() subroutine is in.

PS. Is there any reason why you are calling the function with a different name (fChangeFieldName <> fChangeFieldCaption)?
Jun 15 '09 #11
Davbib
5
Ah. found it. I was calling fchangefieldname, not fchangefieldcaption! It works now!

Thanks so much for the help. I've put over 20 hours into this little problem.
Jun 15 '09 #12
ADezii
8,834 Expert 8TB
@Davbib
The Error was on my part, not yours. In Post #2 the actual Function Definition in Item #1 does not agree with the Calling Procedures that I had given you in Items #2 and #3. Sorry! I was experimenting with different Options in order to try to find a workable solution to your problem, but unfortunately, did not sync them up!
Jun 15 '09 #13
Davbib
5
@ADezii
No problem at all. I appreciate the help and since I did manage to find it on my own, I look at it as you giving me a learning experience on debugging code. So, with your help I learned two things today :)
Jun 15 '09 #14

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

Similar topics

4
by: Sims | last post by:
Hi, This will validate, (http://validator.w3.org/) <form method="POST" action="" name='xForm' style='background:inherit;'> <table style='background:inherit; width:100%;'> <tr width=80%> <td>...
4
by: lionel | last post by:
Hello yall, I can't find a way to center a <fo:table> element. I have tried many attributes (display-align,start-indent,padding-start,...). I have tried to encapslulate it in a <fo:block>...
17
by: FDYocum | last post by:
I am having problems with a Web site that I've designed and I am grinding my teeth in frustration. The pages are built around a table with four cells. The first cell is spanned two rows and is...
9
by: Wang, Jay | last post by:
I try to group several rows in a table into a div and show/hide them by click on a button somewhere with a javascript link. When clicked, the link will toggle the style of the div section's style...
1
by: altacct | last post by:
I've got a multi-page subform with a tab control on one of the pages. The code that moves you from page three to page four, which is where the tab is, changes the tab captions according to some...
3
by: D. Shane Fowlkes | last post by:
I have a Datagrid which in theory, should allow you to edit and update the records. I've stripped my test page down so that it's only attempting to update one field - "description". Yet when I...
3
by: Chifo | last post by:
hello. i have a problem with a populate html table with data from table here it's the problem two querys retrieving data from table, one of querys show me a colletion of data from 6:00 am to...
5
by: Slavan | last post by:
I have an update statement that I'm executing against Oracle database from my C# code and it won't work. UPDATE MenuCaptions SET Caption = N@Caption WHERE MenuId = @MenuId AND CultureId =...
2
by: webbi | last post by:
I have a text box called txtAttirubte1 and an update button in FormA. I want to update the label caption for all label name Attribute1_Lable in FormB and FormC with the WORD I type in the FormA text...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.