Results 1 to 13 of 13

Thread: Access DB Row Values into Columns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Access DB Row Values into Columns

    Hi I am Working Windows Appl .Net08.
    My DB is Access Db.
    The below image is from Access DB only.
    In the image the table called Contacts having one of the column called 'ContactMedium'. In this Column having values like 'Mobile' , 'email'.
    Now i want to saperate these values and create new columns in the same table.
    How can make these values as a new columns ??

    If i create Crosstab Query it giving error like Value type is not Aggr.

    May i know the solution ??

    Thanks
    Attached Images Attached Images  
    Last edited by malatesh kumar; Sep 23rd, 2011 at 03:42 AM.

  2. #2
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: Access DB Row Values into Columns

    hi mala try this
    vb Code:
    1. Public Class Form1
    2.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Try
    4.             ClassDatabase.CreateColumns()
    5.         Catch ex As Exception
    6.             MsgBox(ex.ToString)
    7.         End Try
    8.     End Sub
    9. End Class
    10.  
    11.  
    12. Public Class ClassDatabase
    13.     ''' <summary>
    14.     ''' give your database file name
    15.     ''' </summary>
    16.     Public Shared ReadOnly Property DatabasefileName() As String
    17.         Get
    18.             Return "E:\People.mdb"
    19.         End Get
    20.     End Property
    21.     ''' <summary>
    22.     ''' this is your connection string
    23.     ''' </summary>
    24.     Private Shared ReadOnly Property ConnectionString() As String
    25.         Get
    26.             Return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabasefileName & ";"
    27.         End Get
    28.     End Property
    29.  
    30.  
    31.     Public Shared Function CreateColumns() As Boolean
    32.         Try
    33.             Dim _datasValue As New List(Of String)
    34.             Using _oledbConxn As New OleDb.OleDbConnection
    35.                 _oledbConxn.ConnectionString = ConnectionString
    36.                 _oledbConxn.Open()
    37.                 Dim _sql$ = "SELECT * FROM tblContacts"
    38.                 Dim Rdr As OleDb.OleDbDataReader
    39.                 Using _cmd As New OleDb.OleDbCommand
    40.                     _cmd.Connection = _oledbConxn
    41.                     _cmd.CommandText = _sql$
    42.                     Rdr = _cmd.ExecuteReader
    43.                 End Using
    44.                 If Rdr.HasRows Then
    45.                     While Rdr.Read
    46.                         _datasValue.Add(Rdr.Item(7).ToString) 'thi is your 'ContactMedium' columnindex
    47.                     End While
    48.                 End If
    49.                 Rdr.Close()
    50.             End Using
    51.  
    52.             'check and Create Columns
    53.             For Each _itmstr$ In _datasValue
    54.                 If ColumnExists(_itmstr$.ToString) = False Then
    55.                     _itmstr$ = _itmstr$.Replace(" ", "")
    56.                     CreateTable("tblContacts", _itmstr$)
    57.                 End If
    58.             Next
    59.             Return True
    60.         Catch ex As Exception
    61.             Return False
    62.         End Try
    63.     End Function
    64.     Private Shared Function ColumnExists(ByVal _columnName$) As Boolean
    65.         Try
    66.             Using _oledbConxn As New OleDb.OleDbConnection
    67.                 _oledbConxn.ConnectionString = ConnectionString
    68.                 Dim _cmd As New OleDb.OleDbCommand("SELECT * FROM tblContacts", _oledbConxn)
    69.                 Dim Da As New OleDb.OleDbDataAdapter(_cmd)
    70.                 Dim Dst As New DataSet
    71.                 Da.Fill(Dst, "tblContacts")
    72.                 'getColumns
    73.                 For Each _clmn As DataColumn In Dst.Tables(0).Columns
    74.                     If _columnName$.ToString.ToLower.Equals(_clmn.ColumnName.ToLower) = True Then
    75.                         Return True
    76.                     End If
    77.                 Next
    78.                 _cmd.Dispose()
    79.                 Da.Dispose()
    80.                 Dst.Dispose()
    81.             End Using
    82.             Return False
    83.         Catch ex As Exception
    84.             Return False
    85.         End Try
    86.     End Function
    87.     Private Shared Function CreateTable(ByVal _tableName$, ByVal _newColumnName$) As Boolean
    88.         Try
    89.             If ColumnExists(_newColumnName) = True Then Return True
    90.             Using _oledbConxn As New OleDb.OleDbConnection
    91.                 _oledbConxn.ConnectionString = ConnectionString
    92.                 _oledbConxn.Open()
    93.                 Dim _sqlCreateTable$ = "ALTER TABLE " & _tableName$ & " ADD COLUMN " & _newColumnName$ & " STRING"
    94.                 Using _cmd As New OleDb.OleDbCommand
    95.                     _cmd.Connection = _oledbConxn
    96.                     _cmd.CommandText = _sqlCreateTable$
    97.                     _cmd.ExecuteNonQuery()
    98.                 End Using
    99.             End Using
    100.             Return True
    101.         Catch ex As Exception
    102.             Return False
    103.         End Try
    104.     End Function
    105. End Class

  3. #3
    Addicted Member
    Join Date
    Nov 2010
    Location
    TamilNadu, India
    Posts
    249

    Re: Access DB Row Values into Columns

    this is the preview for my above codes....

    Name:  Img.jpg
Views: 243
Size:  332.3 KB

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Access DB Row Values into Columns

    do you want a permanent solution? or simply a view that represents the data?

    medsoft's method will work if you want a permanent solution... but it's a bit heavy handed...

    Code:
    SELECT C.Title, C.FirstName, C.LastName, C.Street, .... , PHONE.ContactNumber as PhoneNumber, EMAIL.ContactNumber as EmailAddress 
    FROM tblContacts C
    left join tblContacts PHONE on C.ID = PHONE.ID and PHONE.ContactMedium = 'Mobile'
    left join tblContacts EMAIL on C.ID = EMAIL.ID and EMAIL.ContactMedium = 'email'
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Re: Access DB Row Values into Columns

    Hi Thanks for given response.
    No i dont want Permanent Solution, i want simply view table thats it.
    techgnome , as per ur code :
    i wrote same code in my Access DB, but i am getting join Error "Syntax error in join Operation"
    My Code is as below:

    vb Code:
    1. SELECT  C.PersonID, C.EventName, C.Surname, C.GivenName, C.InstitutionName, PhysicalAddress.ContactNumber as PA
    2. FROM tblTEMP AS C RIGHT JOIN tblTEMP ON C.PersonID = tblTEMP.PersonID
    3. and PhysicalAddress.ContactMedium= 'Physical Address' ;


    Thanks Medsont Thanks techgnome..

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Access DB Row Values into Columns

    first. it should be a LEFT join, not a right....

    secondly, where did "PhysicalAddress" come from? You didn't alias any of the tables as PhysicalAddress... it should be just tblTemp, both in the join and in the select...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Re: Access DB Row Values into Columns

    My i know as per ur code (#4) what is PHONE indicates.....
    Last edited by malatesh kumar; Sep 26th, 2011 at 11:23 PM.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Re: Access DB Row Values into Columns

    B'se i am Confusing where i want to fill the Column names otherwise can u send me the Syntax of this query....

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Re: Access DB Row Values into Columns

    Hi i am getting the new columns Successfully..
    The image is showing i am getting new columns.
    But the problem is the number of rows are increased .
    How can i delete the duplicates .

    My SQL Query is as Below:

    Code:
    SELECT C.PersonID, C.EventName, C.Type, C.StartDate, C.EndDate, C.Venue, C.OrganizedBy, C.Surname, C.GivenName, C.Gender, C.Nationality, C.InstitutionName, C.InstitutionType, C.JobStatus, 
    C.CurrentDesignation, C.Role, [Land-Telephone].ContactNumber AS LPHONE, PhysicalAddress.ContactNumber AS Address, FAX.ContactNumber AS FAX, C.City, C.State, C.Country
    FROM ((tblTEMP AS C LEFT JOIN tblTEMP AS [Land-Telephone] ON C.PersonID = [Land-Telephone].[PersonID]) 
    LEFT JOIN tblTEMP AS PhysicalAddress ON C.PersonID = PhysicalAddress.[PersonID]) 
    LEFT JOIN tblTEMP AS FAX ON C.PersonID = FAX.[PersonID]
    WHERE ((([Land-Telephone].ContactMedium)='Land-Telephone') AND ((PhysicalAddress.ContactMedium)='PhysicalAddress') AND ((FAX.ContactMedium)='FAX'));
    Thanks

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Re: Access DB Row Values into Columns

    the image is
    Attached Images Attached Images  

  11. #11
    New Member
    Join Date
    Jun 2010
    Posts
    4

    Re: Access DB Row Values into Columns

    use DISTINCT Keyword

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    May 2010
    Posts
    229

    Re: Access DB Row Values into Columns

    HI Before Distinct Command i got some thousands of rows came but after apply the Distinct i am getting 30 Rows. the rows are reduced but actually my output rows should be 9 only. i want to remove few more duplicate records.

    In my Temp Table Some persons having only Physical Address but some having both Physical Address as well as LandPhone and Fax...
    So for each personID it displays 2 or more records.

    May be it Join Problem... I am not understanding.

    Thanks

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Access DB Row Values into Columns

    First, PHONE is an alias for the table. Because in the sample, I joined the table to itself, each one has to have a different name...

    Secondly, you're getting multiple lines because each contact has multiple rows in the table...

    Thirdly, DISTINCT isn't the solution in this case... but will probably suffice given the info at hand, and trying to keep it simple.

    Lastly... with out knowing more about the data and the table's design... I don't know what to tell you about eliminating the duplicate rows... other than to ask, does every contact have a physical address line? Your comment about some having only a physical address would suggest that, but your screen shots say otherwise.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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