Form is ignoring the code in OnOpen event

2019-07-24 18:56发布

问题:

Have a new form that contains controls and is linked to a table in SQL Server. Have code that relinks the table on open, and that code works in 5 other apps, but not this one. Here is the code:

  Dim tdf As DAO.TableDef
  For Each tdf In CurrentDb.TableDefs
  ' check if table is a linked table
  If Len(tdf.Connect) > 0 Then
  tdf.Connect = "Connect String"
  tdf.RefreshLink
  End If
  Next

I had the form set up in options to open when the app is activated, but it ask for the password which I have in the connect string. I have entered a stop on a line of code in the in open event, but that code never executes. It's like the onopen event is being ignored.

Thanks jpl

回答1:

Access wants to load the data of a bound form before the On Open event runs. I guess in your other applications the start form is unbound.

Your best way around this is:

Create an AutoExec macro that calls a public function with your relink code.

After that the function can open your bound start form