Solved

Cycle through Access table and retrieve field

Posted on 2014-03-27
6
176 Views
Last Modified: 2014-04-15
I have a Microsoft access database (2007) which has a contacts table containing company addresses.
To help stop users adding duplicates, one of the fields I was checking was postcode (zipcode). I am currently using Dcount to see if there are any duplicates, a msgbox alerts user that there is x number of Companies with that postcode.

Is it possible to extract the Company names from database where the postcode = 'postcode entered on userform' so the msgbox can advise a list of companies in the database with same postcode.

The user would then check if the new company being added has already been added by checking the list (maybe company name spelt a little differently)
0
Comment
Question by:foxpc123
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39960901
Try this:

Sub Demo()

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Select * FROM tblYourTable WHERE postcode =" & Me.txtpostcode)

    MsgBox rs!postcode
    
    rs.Close
    Set rs = Nothing
    Set db = Nothing

End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 39961865
You could most easily create a multi-column unique index on the contacts table.  For example, you could make company ID (?name?) and address type a unique tuple.  If the user tries to add a second address type for a company, the Insert will raise a trappable duplicate key error.
0
 
LVL 3

Author Comment

by:foxpc123
ID: 39962167
MacroShadow, that only seems to pick up the first instance of the postcode.

I tried code below also to give Business name, but only first business shown in msgbox:

    
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Select [Business name] FROM [tbl - Contact Details] WHERE [Post Code]='" & Me.Post_Code & "'")


    
MsgBox (rs![Business name])

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:MacroShadow
MacroShadow earned 300 total points
ID: 39964131
Of course! You're not supposed to have duplicates.

Anyway, if the is what you want try this:
Sub Demo()

    Dim db As DAO.Database
    Dim rs As DAO.Recordset

    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Select [Business name] FROM [tbl - Contact Details] WHERE [Post Code]='" & Me.Post_Code & "'")

    rs.MoveLast
    rs.MoveFirst
    Do Until rs.EOF
        MsgBox (rs![Business name])
        rs.MoveNext
    Loop
    
    rs.Close
    Set rs = Nothing
    Set db = Nothing

End Sub

Open in new window

0
 
LVL 3

Accepted Solution

by:
foxpc123 earned 0 total points
ID: 39992524
This was the code I ended up using

    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("Select [Business name] FROM [tbl - Contact Details] WHERE [Post Code]='" & Me.Post_Code & "'")
    

    rs.MoveLast
    rs.MoveFirst
    Do Until rs.EOF
     Companylist = Companylist & vbCrLf & (rs![Business name])
        rs.MoveNext
    Loop

thisclient = DLookup("[Business name]", "[tbl - Contact details]", "[Post Code] = '" & Me.Post_Code & "'")


      Response = MsgBox("Postcode " & Me.Post_Code & " already exists in database for:-" & vbCrLf & Companylist & vbCrLf & vbCrLf & "Do you want to continue Adding this postcode ?", vbYesNo + vbQuestion, "Confirm")
        rs.Close
    Set rs = Nothing
    Set db = Nothing

Open in new window


Thanks for the help
0
 
LVL 3

Author Closing Comment

by:foxpc123
ID: 40001120
Final solution gives one alert box, with list of companies with added postcode.

Can have duplicate postcodes, as in UK postcode is not unique to one building.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Cloud-based technologies and services will continue to grow in popularity in 2017 thanks to the simple, scalable and cost-effective solutions they deliver. Here are three areas where cloud adoption is poised to really take off.
As cyber crime continues to grow in both numbers and sophistication, a troubling trend of optimization has emerged over the last year.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Experts Exchange lesson shows how to use VBA to loop through rows in Excel.  In order to sort, filter, and use database features, there needs to be a value in each column for every row. When data arrives with values missing, code to copy values…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question