Advertisement
Mikestriken

GPT Paste

Nov 20th, 2024 (edited)
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub AddTypDtyFieldsToAnotherTable()
  2.     Dim db As DAO.Database
  3.     Dim tdf As DAO.TableDef
  4.     Dim fld As DAO.Field
  5.     Dim srcTableName As String
  6.     Dim destTableName As String
  7.     Dim insertSQL As String
  8.    
  9.     ' Define the source and destination table names
  10.    srcTableName = "YourSourceTableName" ' Replace with your source table name
  11.    destTableName = "YourDestinationTableName" ' Replace with your destination table name
  12.  
  13.     ' Get a reference to the current database
  14.    Set db = CurrentDb
  15.  
  16.     ' Ensure the destination table exists
  17.    If Not TableExists(destTableName) Then
  18.         MsgBox "Destination table '" & destTableName & "' does not exist.", vbExclamation
  19.         Exit Sub
  20.     End If
  21.  
  22.     ' Get the source table definition
  23.    On Error Resume Next
  24.     Set tdf = db.TableDefs(srcTableName)
  25.     If Err.Number <> 0 Then
  26.         MsgBox "Source table '" & srcTableName & "' does not exist.", vbExclamation
  27.         Exit Sub
  28.     End If
  29.     On Error GoTo 0
  30.  
  31.     ' Loop through the fields in the source table
  32.    For Each fld In tdf.Fields
  33.         If Right(fld.Name, 7) = "_TypDty" Then
  34.             ' Create SQL to insert the field name into the destination table
  35.            insertSQL = "INSERT INTO " & destTableName & " (FieldNameColumn) VALUES ('" & fld.Name & "')"
  36.             db.Execute insertSQL, dbFailOnError
  37.         End If
  38.     Next fld
  39.  
  40.     ' Clean up
  41.    Set fld = Nothing
  42.     Set tdf = Nothing
  43.     Set db = Nothing
  44.  
  45.     MsgBox "Fields ending with '_TypDty' have been added to the destination table.", vbInformation
  46. End Sub
  47.  
  48. ' Helper function to check if a table exists
  49. Function TableExists(tableName As String) As Boolean
  50.     Dim tdf As DAO.TableDef
  51.     On Error Resume Next
  52.     Set tdf = CurrentDb.TableDefs(tableName)
  53.     TableExists = Not tdf Is Nothing
  54.     Set tdf = Nothing
  55.     On Error GoTo 0
  56. End Function
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement