Visual Studio 2010 - Database/MS ACCESS errors ('TextBox1' is not declared. It may be inaccessible due to its protection level.) RRS feed

  • Question

  • Hello! I would like to ask if want is the solution in this errors. I already tried the error correction options in which I have to generate methods stub for 'TextBox1' in 'PrelimExam.UpdateInformation'. but it never works...Hope someone could help me..:)

    Here are the specify code that I've been making:

    Imports System.Data.OleDb
    Public Class UpdateInformation
        Private Sub BtnUpdated_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdated.Click
            Dim dr As New DialogResult
            dr = MessageBox.Show("Are you sure you want to Update?", "Change Details", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
            If dr = Windows.Forms.DialogResult.OK Then
                If con.State = ConnectionState.Closed Then con.Open()
                Dim st As String = "UPDATE [PersonalInfo] SET IDNumber = '" & TxtIDNumber.Text & _
                                    "', FirstName = '" & TxtFirstName.Text & "', LastName = '" & TxtLastName.Text & _
                                    "', MiddleName = '" & TxtMiddleName.Text & "', Address = '" & TxtAddress.Text & _
                                    "'  WHERE IDNumber = '" & PersonalInformation.DataGridView1.SelectedCells(0).Value & "'"
                MessageBox.Show("Process Successful!", "Edit", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Dim cmd As New OleDbCommand(st, con)
            End If
        End Sub

    And here are the errors:

    Error	6	'TxtIDNum' is not declared. It may be inaccessible due to its protection level.	
    Error	7	'TxtFirstName' is not declared. It may be inaccessible due to its protection level.	
    Error	8	'TxtLastName' is not declared. It may be inaccessible due to its protection level.	
    Error	9	'TxtMiddleName' is not declared. It may be inaccessible due to its protection level.	
    Error	10	'TxtAddress' is not declared. It may be inaccessible due to its protection level.	
    They all have the same errors but I cant pin point where should i change my codes.

    Monday, July 24, 2017 4:19 AM

All replies

  • Hi! I almost finishing my code in visual basic but there is an errors keep popping out. I already search for the result but no luck. Wish you can help me. here are the codes and the errors. :)

    Public Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click UpdateInformation.TxtIDNum.Text = Me.DataGridView1.SelectedCells(0).Value.ToString UpdateInformation.TxtFirstName.Text = Me.DataGridView1.SelectedCells(1).Value.ToString UpdateInformation.TxtLastName.Text = Me.DataGridView1.SelectedCells(2).Value.ToString UpdateInformation.TxtMiddleName.Text = Me.DataGridView1.SelectedCells(3).Value.ToString UpdateInformation.TxtAddress.Text = Me.DataGridView1.SelectedCells(4).Value.ToString UpdateInformation.Show() End Sub

    Error	1	'TxtIDNum' is not a member of 'PrelimExam.UpdateInformation'.	
    Error	2	'TxtFirstName' is not a member of 'PrelimExam.UpdateInformation'.	
    Error	3	'TxtLastName' is not a member of 'PrelimExam.UpdateInformation'.	
    Error	4	'TxtMiddleName' is not a member of 'PrelimExam.UpdateInformation'.	
    Error	5	'TxtAddress' is not a member of 'PrelimExam.UpdateInformation'.	

    UpdateInformation is an another form, I have to link it in my first form which is named PersonalInformation. So, Information to UpdateInformation then reversed.
    Here are the Error correction options in one of the textbox named TxtIDNum:
    Generate method stub for 'TxtIDNum' in 'PrelimExam.UpdateInformation'
    Generate Property stub for 'TxtIDNum' in 'PrelimExam.UpdateInformation'
    Generate field for 'TxtIDNum' in 'PrelimExam.UpdateInformation'

    Monday, July 24, 2017 4:11 AM
  • Hi Xela_1998,

    According to your error message, do you have some Textbox controls that these names property are TxtIDNum, TxtFirstName, TxtLastName, TxtMiddleName, TxtAddress? If not , please put some TextBox control and change these name property.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, July 24, 2017 8:35 AM
  • Xela,

    If you make a class, then the class has to know all information which it used. 

    In your case it is better to remove the code not inside a separate class, if you are further with this subject you will do it complete different and also use parameters (which does not change anything to your current problem)

    Therefore simply put this code in your form class as long as you don't understand this completely. 

    Also the button gives probably an error but that we don't see because you show only 6 to 10


    Monday, July 24, 2017 9:24 AM
  • Hello,

    There are several things that you should do to improve your code.

    First off your button click event should be in the form, not in the class. You should ask the user if they want to update in the click event and if they press yes create an new instance of the class and call a function such as below where you pass in values from the TextBox controls to the function. Note the function has it's own connection, this is better than creating one connection which is used many times.

    Note the use of parameters, this is much better than using string concatenation to create your SQL statement as one common issue without parameters is apostrophes in values which will cause runtime errors then there is the case of sql injection.

    If for any reason the operation fails with an exception you can inspect the property Exception in the click event which called the update operation.

    You can do the fill in the button click event if the function returns true.

    Example to learn from

    Public Class SampleOperations
        Private Builder As New OleDbConnectionStringBuilder With
            .Provider = "Microsoft.ACE.OLEDB.12.0",
            .DataSource = IO.Path.Combine(Application.StartupPath, "Database1.accdb")
        Public Property HasError As Boolean
        Public Property Exception As Exception
        Public Function UpdateCustomer(ByVal CustomerId As Integer, ByVal Name As String, ByVal Contact As String) As Boolean
            Dim Success As Boolean = True
            Dim Affected As Integer = 0
            HasError = False
                Using cn As New OleDbConnection With {.ConnectionString = Builder.ConnectionString}
                    Using cmd As New OleDbCommand With {.Connection = cn}
                        cmd.CommandText = "UPDATE Customer SET CompanyName = @CompanyName, ContactName = @ContactName WHERE Identifier = @Identifier"
                        cmd.Parameters.AddWithValue("@CompanyName", Name)
                        cmd.Parameters.AddWithValue("@ContactName", Contact)
                        cmd.Parameters.AddWithValue("@Identifier", Contact)
                        Affected = cmd.ExecuteNonQuery()
                        If Affected = 1 Then
                            Success = True
                        End If
                    End Using
                End Using
            Catch ex As Exception
                HasError = True
                Success = False
                Me.Exception = Exception
            End Try
            Return Success
        End Function
    End Class

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cor Ligthert Monday, July 24, 2017 9:53 AM
    Monday, July 24, 2017 9:48 AM