How do I save the printer setup after Open Printer Dialog?

But is that code pushing it out to your own sql lite database file?
I was just using a text file located in the Application Support folder.

// save it
dim oPageSetup as new PrinterSetup
oPageSetup.MaxHorizontalResolution = 300
oPageSetup.MaxVerticalResolution = 300
app.PrinterSettings = DecodeBase64(app.PlanIni.Section(“Printing”).Attribute(“prtSettingsString”).Value)
if app.PrinterSettings <> “” Then
oPageSetup.SetupString= app.PrinterSettings
End if

// retrieve it and preset the printer dialog with saved settings

if oPageSetup.PageSetupDialog then
  app.PrinterSettings=oPageSetup.SetupString
  // save to INI - UPDATE - TEST
  app.PlanIni.Section("Printing").Attribute("prtSettingsString").Value =  EncodeBase64(app.PrinterSettings)
  app.PlanIni.Save
  g = OpenPrinterDialog(oPageSetup)
end if

This approach does not work though. I am saving to a file that has other settings in it as well.
Was thinking of saving it to its OWN file and naming the file with the printer name maybe? But not sure if that would have bad characters in the printer name so that it could not be used as a file name on a folderitem.

Why not use SQLite. After all it is really just a file. I have a settings table with ID, Name, Value.

I have the methods to read and write from settings. New settings are added similar to a dictionary.

'create and save a new setting
Setting("Printer Setting") = ps.SetupString

'retrieve the setting
ps.SetupString = Setting("Printer Setting").StringValue
Function Setting(Name as string) As DatabaseField
'Retrieve a setting from the database

  SQLps = SettingsDB.Prepare("Select * From Settings where Name like ?")
  SQLps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
  dim rs as RecordSet = SQLps.SQLSelect(Name)
  if SettingsDB.Error then MsgBoxT SettingsDB.ErrorMessage
  if rs <>nil and rs.RecordCount > 0 then
    return rs.Field("Value")
  else
    SQLps = SettingsDB.Prepare("INSERT INTO Settings (Name, Value) VALUES (?,?)")
    SQLps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
    SQLps.BindType(1, SQLitePreparedStatement.SQLITE_TEXT)
    SQLps.SQLExecute(Name,"")
    SQLps = SettingsDB.Prepare("Select * From Settings where Name like ?")
    SQLps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
    rs = SQLps.SQLSelect(Name)
    if SettingsDB.Error then MsgBoxT SettingsDB.ErrorMessage
    if rs <> nil and rs.RecordCount > 0 then
      return rs.Field("Value")
    else
      MsgBoxT "I don't know what is wrong! Problem in Settings module."
    end if
  end if
End Function


Sub Setting(Name as string, Assigns Value as Variant)
'Assign a setting - Create a new setting if it doesn't already exist

  SQLps = SettingsDB.Prepare("Select * From Settings where Name like ?")
  SQLps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
  dim rs as RecordSet = SQLps.SQLSelect(Name)
  if SettingsDB.Error then MsgBoxT SettingsDB.ErrorMessage
  if rs = nil or rs.RecordCount = 0 then
    'if setting is not in table add it
    SQLps = SettingsDB.Prepare("INSERT INTO Settings (Name, Value) VALUES (?,?)")
    SQLps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
    SQLps.BindType(1, SQLitePreparedStatement.SQLITE_TEXT)
    SQLps.SQLExecute(Name,Value)
    if SettingsDB.Error then MsgBoxT SettingsDB.ErrorMessage
    
  Else
    'otherwise update it
    SQLps = SettingsDB.Prepare("UPDATE Settings SET Value = ? where Name = ?")
    SQLps.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
    SQLps.BindType(1, SQLitePreparedStatement.SQLITE_TEXT)
    SQLps.SQLExecute(Value.StringValue,Name)
    if SettingsDB.Error then MsgBoxT SettingsDB.ErrorMessage
  end if
End Sub

So if someone has 3 printers do you store 3 different settings strings one for each printer?

I’d prefer not to use sql lite simply because I already am using a text file for various settings, it’s already working, and I just don’t have time to go through and recode it and if I make any errors then a bug would crop up in code that already works. But the printer setting string does not seem to save and reload properly so I may indeed have to try your method. Someone else was just pushing the Encode64 version to a text file so frankly was going to try that first… different file than the one my other settings are added to.

I do. I also save settings specific to user/computer.
I have multiple printers connected. Settings are save specific to the label or report being printed. I give the user the option to use existing settings or not.

I extract the printer name from the setup string and display for the user so he knows if it’s printing to the correct printer.

Public Function ExtractPrinterName(PrinterSetupString as String) as String dim PrinterName as String if PrinterSetupString.InStr(0,"DevModeStructurePS=") > 0 Then PrinterName = PrinterSetupString.right(PrinterSetupString.Len - (PrinterSetupString.InStr(0,"DevModeStructurePS=") + 18)) PrinterName = PrinterName.Left(PrinterName.InStr(0,chr(0))-1) end if Return PrinterName End Function

Keep using the existing files for now, and simply add the SQLite settings database. Using the methods I provided adding and accessing settings is as easy as reading and writing from a dictionary. I simply connect the settings db when open the app. And your ready to go.

One line to add a new setting or update. (same code)

setting("Setting Name") = variant 'integer, string...

And one line to retrieve a setting. If the setting doesn’t existing yet is created with a blank string “”.

variable = setting("Setting Name").StringValue

Or

If Setting("License Expired").BooleanValue Then Quit