1

I have a problem. I need to take the newly created table column (Field) I had just name "ID" and INDEX it (Unique). I have tried several ways with no success. All I'm asking is for someone to offer a fresh perspective. ~ Shaw

Public Sub OrcleJEtoUnmatched()
Dim db As Database
Dim tdf As DAO.TableDef
Dim fld1 As DAO.Field, fld2 As DAO.Field
Dim idx As Index
Dim rst As DAO.Recordset
Dim hertz As String

Set db = CurrentDb()

'Copies table data from ORACLE JE Table; Creates / overwrites existing data to UNMATCHED Table (Working Table)
DoCmd.RunSQL "SELECT [Oracle JE].* INTO Unmatched FROM [Oracle JE];"

Set tdf = db.TableDefs("Unmatched")
Set fld1 = tdf.CreateField("ID", dbText, 255)
Set fld2 = tdf.CreateField("BatchCalc", dbText, 255)
With tdf
  .Fields.Append fld1
  .Fields.Append fld2
End With

Set rst = db.OpenRecordset("Unmatched", dbOpenTable)
Do Until rst.EOF
  hertz = rst![Accounting Document Item] & Mid(rst![JE Line Description], 20, 2) & Round(Abs(rst![Transaction Amount]), 0)
  rst.Edit
  rst!ID = Replace(hertz, " ", "")
  rst!BatchCalc = Mid(rst![JE Line Description], 8, 8)
  rst.Update
  rst.MoveNext
Loop
rst.Close
Application.RefreshDatabaseWindow

Set fld1 = Nothing
Set fld2 = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub

3 Answers 3

1

If you want to stick with the DAO object model to create your field and index it, look at the Index.CreateField Method in Access' help system.

But I think it's easier to do both by executing an ALTER TABLE statement ...

CurrentDb.Execute "ALTER TABLE Unmatched ADD COLUMN ID TEXT(255) UNIQUE;"
5
  • Using your answer. How about this? DoCmd.RunSQL CREATE UNIQUE INDEX ID ON Unmatched (ID) WITH PRIMARY
    – John Shaw
    Commented Feb 8, 2016 at 18:06
  • 1
    You would have to enclose the statement in quotes. I didn't realize you wanted it to be a primary key instead of just unique. You could still do that with one statement instead of creating the field and index separately: "ALTER TABLE Unmatched ADD COLUMN ID TEXT(255) PRIMARY KEY;"
    – HansUp
    Commented Feb 8, 2016 at 18:10
  • Thank you @HansUp for being there to help us. My job depends on my ability to provide quick work-arounds and sometimes I just get stuck...
    – John Shaw
    Commented Feb 8, 2016 at 18:12
  • 1
    Hold the phone! I just remembered you can't have Nulls in a primary key field. So you would first have to create the ID field and populate it with unique values before you could make it a primary key. Sorry I didn't notice that sooner. :-(
    – HansUp
    Commented Feb 8, 2016 at 18:15
  • I added DoCmd.RunSQL CREATE UNIQUE INDEX ID ON Unmatched (ID) WITH PRIMARY to the code in the space just before Set fld1 = Nothing
    – John Shaw
    Commented Feb 8, 2016 at 18:26
1

To add an index to a DAO tabledef object, use the createindex method.

Here's the basic documentation;

https://msdn.microsoft.com/EN-US/library/office/ff196791.aspx

1
  • thank you brother. I appreciate the link. It's been bookmarked and added to my references.
    – John Shaw
    Commented Feb 8, 2016 at 18:14
0

Using the Answer that @HansUp provided, I altered it and made it work. Thank You both @jhTyppeny & @Hansup for the quick reply.

DoCmd.RunSQL "CREATE UNIQUE INDEX ID ON Unmatched (ID)  WITH PRIMARY"
3
  • 1
    Doesn't that give you error #3058, "Index or primary key cannot contain a Null value"?
    – HansUp
    Commented Feb 8, 2016 at 18:19
  • No, Sir. It actually works. I added the code DoCmd.RunSQL CREATE UNIQUE INDEX ID ON Unmatched (ID) WITH PRIMARY just before Set fld1 = Nothing and it works after this field has been populated with data resulting from the loop.
    – John Shaw
    Commented Feb 8, 2016 at 18:25
  • 1
    @HansUp: I don't think that this is worth celebrating - it smacks of black-box all over.
    – user1945782
    Commented May 19, 2016 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.