2016-11-29 34 views
0

我有綁定到不同的recordSource子窗體綁定窗體,雖然這些recordSources可以通過一個字段鏈接。MS-Access form:奇怪的行爲

我有一個未綁定的文本框控件在我的主窗體和一個按鈕控件,自定義VBA代碼附加到它的OnClick事件。

我想達到的目標是這樣的:(爲便於閱讀僞代碼)

private sub button_Click() 
    'Find the record with the content of the text-box control as its value in FieldA 
    find(mainForm.RecordSource, fieldA, text-box.value) 
    if not found then 
    'Create a record in the subform.recordSource with PK value = text-box.value 
     createDefaultRecord(subform.Form.RecordSource,text-box.value) 
    end if 
    'load the values in the record in the main record source onto the record in the subform recordSource 
    Me!subform.Form.value = mainForm.recordThatMatched.value 
Exit Sub 

實際上我得到它的工作,但有一個小麻煩:如果我介紹碼還沒有產生其記錄在subform.RecordSource,它創建它,然後失敗如果您嘗試負載不同的代碼或關閉的形式加載它然後再次加載第一個代碼IT WORKS!

任何想法爲什麼發生這種情況或如何編碼解決這種情況?它確實在擾亂我。我曾嘗試加載記錄之前運行acSaveRecordacSave但結局是一樣的:我不能子窗體上加載最近創紀錄的創建,無需先導航通過主窗體的記錄(這是有點愚蠢和反直覺)

我的診斷可能是錯誤的,也可以指出。我對Access和VBA沒有太多的經驗。

+1

我的猜測:添加記錄後需要一個「Me!subform.Form.Requery」。如果你顯示實際的代碼會更容易。 – Andre

+0

就是這樣。由於我通過快速教程學習了VBA,因此我缺少其「內置函數」的一些基本知識。非常感謝! :d – Feillen

回答

1

的解決辦法是補充一點:添加記錄後

Me!subform.Form.Requery 

表單(或子表單)不會在其記錄源中顯示新添加的記錄,除非它被重新查詢(手動使用Shift + F9或通過代碼)。