' migrate_isbusiness_to_households.vbs ' Moves IsBusiness from HouseholderNames to Households. ' ' Usage: ' cscript //nologo scripts\migrate_isbusiness_to_households.vbs "C:\path\to\myAccessFile.accdb" ' ' What it does: ' 1) Adds Households.IsBusiness (SMALLINT) if missing ' 2) Copies data: sets Households.IsBusiness=1 if any related HouseholderNames.IsBusiness<>0 ' 3) Sets NULLs to 0 ' 4) Drops HouseholderNames.IsBusiness if present ' Option Explicit Dim dbPath If WScript.Arguments.Count < 1 Then WScript.Echo "ERROR: missing db path." WScript.Echo "Usage: cscript //nologo scripts\migrate_isbusiness_to_households.vbs ""C:\path\to\db.accdb""" WScript.Quit 1 End If dbPath = WScript.Arguments(0) Dim conn Set conn = CreateObject("ADODB.Connection") conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & dbPath & ";Persist Security Info=False;" On Error Resume Next If Not ColumnExists(conn, "Households", "IsBusiness") Then Exec conn, "ALTER TABLE [Households] ADD COLUMN [IsBusiness] SMALLINT" If Err.Number <> 0 Then WScript.Echo "ERROR adding Households.IsBusiness: " & Err.Description WScript.Quit 1 End If WScript.Echo "Added Households.IsBusiness" Else WScript.Echo "Households.IsBusiness already exists" End If ' Copy data (only if the old column exists) If ColumnExists(conn, "HouseholderNames", "IsBusiness") Then ' Normalize all existing households first so the column is never left NULL. Exec conn, "UPDATE [Households] SET [IsBusiness]=0" If Err.Number <> 0 Then WScript.Echo "ERROR initializing Households.IsBusiness: " & Err.Description WScript.Quit 1 End If ' Promote households to business when any related name was previously marked as a business. Exec conn, "UPDATE [Households] SET [IsBusiness]=1 WHERE [Id] IN (SELECT [HouseholdId] FROM [HouseholderNames] WHERE [IsBusiness]<>0)" If Err.Number <> 0 Then WScript.Echo "ERROR copying IsBusiness to Households: " & Err.Description WScript.Quit 1 End If WScript.Echo "Copied IsBusiness values to Households" Exec conn, "ALTER TABLE [HouseholderNames] DROP COLUMN [IsBusiness]" If Err.Number <> 0 Then WScript.Echo "ERROR dropping HouseholderNames.IsBusiness: " & Err.Description WScript.Quit 1 End If WScript.Echo "Dropped HouseholderNames.IsBusiness" Else WScript.Echo "HouseholderNames.IsBusiness does not exist; nothing to drop" End If conn.Close Set conn = Nothing WScript.Echo "Done." ' --- helpers --- Sub Exec(c, sql) Err.Clear c.Execute sql End Sub Function ColumnExists(c, tableName, colName) Dim rs ColumnExists = False Err.Clear Set rs = c.OpenSchema(4, Array(Empty, Empty, tableName, colName)) ' adSchemaColumns=4 If Err.Number <> 0 Then Err.Clear Exit Function End If If Not rs.EOF Then ColumnExists = True rs.Close Set rs = Nothing End Function