Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

vba - Hide Controls in MS Access Based on Value Field

I am trying to hide some controls in a form in MS Access. The idea is that a linked table has a type of question such as OpenResponse or OptionBox. Given this input I want to switch the type of input the user can input. Here is a sample of what I have:

Private Sub QuestionType_AfterUpdate()

    Dim QType As String

    Set QType = Me.QuestionType.Value

    Select Case QType

        Case OpenResponse
            Forms("Survey").Controls(AnswerField).Visible = True
            Forms("Survey").Controls(OptionTitle).Visible = False
            Forms("Survey").Controls(OptionFrame).Visible = False
            Forms("Survey").Controls(Option69).Visible = False
            Forms("Survey").Controls(Option70).Visible = False
            Forms("Survey").Controls(Option71).Visible = False
            Forms("Survey").Controls(Option72).Visible = False
            Forms("Survey").Controls(Option73).Visible = False
            Forms("Survey").Controls(Option74).Visible = False
            Forms("Survey").Controls(Option75).Visible = False
            Forms("Survey").Controls(Option76).Visible = False
            Forms("Survey").Controls(Option77).Visible = False
            Forms("Survey").Controls(Option78).Visible = False

        Case OptionBox

            Forms("Survey").Controls(AnswerField).Visible = False
            Forms("Survey").Controls(OptionTitle).Visible = True
            Forms("Survey").Controls(OptionFrame).Visible = True
            Forms("Survey").Controls(Option69).Visible = True
            Forms("Survey").Controls(Option70).Visible = True
            Forms("Survey").Controls(Option71).Visible = True
            Forms("Survey").Controls(Option72).Visible = True
            Forms("Survey").Controls(Option73).Visible = True
            Forms("Survey").Controls(Option74).Visible = True
            Forms("Survey").Controls(Option75).Visible = True
            Forms("Survey").Controls(Option76).Visible = True
            Forms("Survey").Controls(Option77).Visible = True
            Forms("Survey").Controls(Option78).Visible = True

    End Select

End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're not thinking "out of the box", here. In this type of environment, thinking "inside the box" can ruin your day.

This is a perfect example of when to use a table-driven approach. The primary question here is; Can/Will the questions ever change? The probable answer is, Yes. In this case, I would do the something like this:

Create a table called Questions. In that table, list the questions (possibly in a Memo field, but a Text field may work), the question type, the control name and the value (True or False). Then, you can query the Questions table based on question type, and loop through the resulting dataset to set the Visible property of your controls. This way, you can always add more questions later if necessary. In fact, you might also add a Primary Key called QuestionID, and a True/False field called Active so you can go back and remove some of the questions without creating orphaned records, and query only questions marked as Active.

If I'm misunderstanding your purpose, let me know and I'll try to edit my answer accordingly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...