Results 1 to 3 of 3

Thread: [RESOLVED] Help! Spaces in Access Fieldnames

  1. #1

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Resolved [RESOLVED] Help! Spaces in Access Fieldnames

    Hi there,

    I understand that one should never put spaces in fieldnames or table tables in access if using VB.Net to access them.

    However, this database was not created by me and I would like to connect and manipulate the data. Please advice. Is it not possible at all? What about Square brackets?

    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
            Try
                Dim strSQL As String
                Dim data_Dataset As New DataSet
                Dim data_adapter As OleDb.OleDbDataAdapter
                Dim dt As New DataTable
      
                Dim i As Integer
                strSQL = "SELECT ["
                For i = 0 To Me.lstFields.SelectedIndices.Count - 1
                    strSQL &= Me.lstFields.Items(Me.lstFields.SelectedIndices(i)) & ","
                Next
                strSQL = strSQL.TrimEnd(",")
                strSQL += "] FROM " & Me.lstTables.SelectedItem.ToString
                MsgBox(strSQL)
                ' Create the SqlDataAdapter.
                data_adapter = New OleDb.OleDbDataAdapter(strSQL, gConnectionString)
                data_adapter.Fill(dt)
                Me.DataGridView1.DataSource = dt
    
                data_adapter.Dispose()
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message.ToString(), "Data Load Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    
            End Try
    
        End Sub
    If you find my thread helpful, please remember to rate me

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help! Spaces in Access Fieldnames

    You just answered your own question:
    What about Square brackets?
    You have to put brackets around each individual column name though, not the whole column list. This:
    Code:
    SELECT [Column1, Column2, Column3] FROM Table1
    is illegal. This:
    Code:
    SELECT [Column1], [Column2], [Column3] FROM Table1
    is correct.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Help! Spaces in Access Fieldnames

    Ahhhh i've resolved my own problem.

    Just put square brackets on each of the field. Also need some information on the best way to format SQL statements?
    If you find my thread helpful, please remember to rate me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width