473,511 Members | 17,164 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add Indexed property "Yes (No Duplicates)" to field in backend table - VBA

547 Contributor
I use this code to add "number fields" to a table in backend database
What i don't have in this code, is to set the Indexed property to "Yes (No Duplicates)"
Any suggestions on how to accomplish that?
Please assist
Expand|Select|Wrap|Line Numbers
  1. Dim Marker As Integer
  2. Dim Rtn As Integer
  3. Dim s As String
  4. Dim dbBackend As DAO.Database
  5. Dim tdf As DAO.TableDef
  6. Dim fld As DAO.Field
  7. Dim prp As DAO.Property
  8. Dim strTableName As String
  9. Dim strFieldName As String
  10. Dim strFieldCaption As String
  11. Dim strFieldNameToCheck As String
  12.  
  13. On Error GoTo Err_Section
  14. Marker = 1
  15. strTableName = "ConsumableSTForms"
  16. MsgBox "Click OK to add some different fields to the table '" & strTableName & "'.", , "Back End Updater"
  17. '* The full path of the back-end database can be set here
  18. gstrBackEndDBName = "C:\pisprg\PisDataV2.accdb"
  19. '* Open the back-end database
  20. If Trim("" & gstrBackEndDBName) = "" Or Dir(gstrBackEndDBName, vbHidden + vbSystem) = "" Then
  21.     Beep
  22.     MsgBox "The back end database '" & gstrBackEndDBName & "' does not exist."
  23.     MsgBox "Try running the first option in the demo. That will set the variable that holds the back-end database name."
  24.     GoTo Exit_Section
  25. End If
  26. Set dbBackend = OpenDatabase(gstrBackEndDBName, True, False)
  27. '* Check for existence of new field, and only continue if password entered
  28. '***************************************************************************
  29. strFieldNameToCheck = "NPItemID"
  30. '* get tabledef
  31. For Each tdf In CurrentDb.TableDefs
  32.     If tdf.Name = strTableName Then
  33.         Exit For
  34.     End If
  35. Next tdf
  36. If tdf.Name <> strTableName Then
  37.     MsgBox "Table '" & strTableName & "' not found. Exiting"
  38.     GoTo Exit_Section
  39. End If
  40. For Each fld In tdf.Fields
  41.     If fld.Name = strFieldNameToCheck Then
  42.         Exit For
  43.     End If
  44. Next fld
  45. If fld Is Nothing Then
  46.     '* OK to proceed
  47. Else
  48.     If fld.Name = strFieldNameToCheck Then
  49.         '* Found it - might not want to run again
  50.         If InputBox( _
  51.             "The new update has been done before.Do not continue. if there is a problem then add the password to continue anyway (the password is '1234').", _
  52.             "Back End Updater", "") = "1234" Then
  53.         Else
  54.             MsgBox "Password invalid. Exiting."
  55.             GoTo Exit_Section
  56.         End If
  57.     Else
  58.         '* OK to proceed
  59.     End If
  60. End If
  61.  
  62. '* Get tabledef
  63. For Each tdf In dbBackend.TableDefs
  64.     If tdf.Name = strTableName Then
  65.         Exit For
  66.     End If
  67. Next tdf
  68. If tdf.Name <> strTableName Then
  69.     MsgBox "Table '" & strTableName & "' not found. Exiting"
  70.     GoTo Exit_Section
  71. End If
  72.  
  73. Marker = 2
  74. '***************************************************
  75. strFieldName = "NPItemID"
  76. On Error Resume Next
  77. tdf.Fields.Delete strFieldName                                              '* Delete it if it already exists
  78. Err.clear
  79. On Error GoTo Err_Section
  80. Set fld = tdf.CreateField(strFieldName, dbDouble)                             '* Create long integer field and set default value
  81. With fld
  82.    '.DefaultValue = ""
  83.     .DefaultValue = "0"
  84.     .Required = False
  85.  
  86. End With
  87. tdf.Fields.Append fld
  88. Set prp = fld.CreateProperty("Caption", dbText, "NPItemID")
  89. fld.Properties.Append prp
  90. Set prp = fld.CreateProperty("Description", dbText, "NPItemID.")
  91. fld.Properties.Append prp
  92. CurrentDb().Execute "Update " & strTableName & " set " & strFieldName & " = true;", dbSeeChanges + dbFailOnError
  93.  
  94. If Not xg_CreateIndex(tdf, strFieldName, False, False, True) Then Beep: GoTo Exit_Section   '* Create index for foreign key field
  95.  
  96. MsgBox "Fields added successfully."
  97.  
  98. Exit_Section:
  99.     On Error Resume Next
  100.     On Error GoTo 0
  101.     Exit Sub
  102. Err_Section:
  103.     Select Case Err
  104.     Case Else
  105.         Beep
  106.         MsgBox "Error (" & Marker & "), object " & Err.Source & ": " & Err.Number & " - " & Err.Description
  107.     End Select
  108.     Err.clear
  109.     Resume Exit_Section
  110.  
Apr 4 '12 #1
3 4422
neelsfer
547 Contributor
If i change line 94 to
Expand|Select|Wrap|Line Numbers
  1. If Not xg_CreateIndex(tdf, strFieldName, False, False, True) Then Beep: GoTo Exit_Section   '* Create index for foreign key field
then i get "Yes(Duplicates OK)"

I can't get "Yes(No duplicates)
Apr 4 '12 #2
neelsfer
547 Contributor
i got it right by changing line 94 to
Expand|Select|Wrap|Line Numbers
  1. If Not xg_CreateIndex(tdf, strFieldName, False, True, True) Then Beep: GoTo Exit_Section   '* Create index for foreign key field
But the table may not have any data in it
Apr 4 '12 #3
NeoPa
32,557 Recognized Expert Moderator MVP
I'm sure you'd find it can make a lot more sense if you were to use Named parameters Neels. Not just for us, but for you too.
Apr 5 '12 #4

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

Similar topics

8
20310
by: Vladimir | last post by:
Hello, I have a table in MS Access database. It has one field (with BYTE datatype) that has several properties set in Lookup tab of table Design View. Display Control = Combo Box. Row Source...
0
1103
by: Mike | last post by:
Hi, I have created a collection of a custom class. Everything works fine (can add these items in a combo from withing a VB.NET application, for instance), but when looking at the collection from a...
1
1819
by: Mike | last post by:
Hi, I have created a collection of a custom class. Everything works fine (can add these items in a combo from within a VB.NET application, for instance), but when looking at the collection from a...
7
2006
by: Ioannis Vranos | last post by:
Fellows there is probably a serious implementation bug of C++/CLI indexed property in VC++ 2005, it looks like it is implemented the opposite way than the C++/CLI draft says! At first the...
1
3361
by: Mike | last post by:
Hi, I have created a collection of a custom class. Everything works fine (can add these items in a combo from within a VB.NET application, for instance), but when looking at the collection from a...
6
1854
by: Altman | last post by:
I would like to use an indexed property to access my private array of user controls. If I don't use an indexed property, the property will show up in my Properties Window during development. Is...
1
1659
by: Ramani Kurella | last post by:
Hello, Iam trying to send x.509 certificate of the client to sign a soap message request and encrypt it with the server's x.509 certificate in a secure conversation policy based. I somehow...
3
1496
by: Michael Matteson | last post by:
I have two classes. Class A and Class B. I give class A 5 properties int prop1(){} int prop2(){} int prop3(){} int prop4(){} classB prop5(){} what i would like to do is to create a 5th...
4
4960
by: Jim Devenish | last post by:
I wish to copy a table on a SQL Server backend to a table on an Access (.mdb) front end in the simplest way. I have the following to get the recordset but am seeking something easier. Dim...
0
1124
by: saudamini | last post by:
i am trying to deserlize XML against the class which has this an indexed property, the deserializer just skips this indexed property, it's not setting it. I remember reading somewhere XML...
0
7245
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
7144
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
7356
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
7512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.