Solved

Copy value in Combo Box(es) & Text Box to table with Macro

Posted on 2010-11-19
22
405 Views
Last Modified: 2012-05-10
Hello Experts,

I would like to use a command button to copy values from a set of combo boxes and text boxes. In this set, I have 2 combo boxes and 1 text box. Once I have selected the required values, I would like to utilise a command button to copy these values to an independent table, say tbl_values. Each time I press the command button, I would require each set of these three values to be copied across to the tbl_values.

However, as a safeguard, I would like to include a message saying "Are you sure you would like to create this combined value of 'cbo_1' with 'cbo_2' on the 'txt_value'? Yes/No."

If the user selects Yes, a message would say "The following values where copied 'cbo_1', 'cbo_2' on the 'txt_value'.

Warmest Regards

LK
0
Comment
Question by:lkirke
  • 8
  • 7
  • 4
  • +1
22 Comments
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 34178716
On the Click event of your command button, place this code:

If MsgBox ("Are you sure you would like to create this combined value of 'cbo_1' with 'cbo_2' on the 'txt_value'?",vbYesNo, "Title") = vbYes Then
Me.txt_Value=Me.cbo_1& " " & Me.cbo_2
Else
DoCmd.CancelEvent
End If

Sincerely,
Ed
0
 

Author Comment

by:lkirke
ID: 34178729
Thank you Ed. My primary question relates to the copying of values. Any ideas? :)
0
 
LVL 39

Expert Comment

by:als315
ID: 34178832
Add some strings to Ed's code:
 
Dim SQL As String
SQL = "INSERT INTO tbl_values ( cbo_1, cbo_2, txt_value )SELECT " & Me.cbo_1 & " AS 1, " & Me.cbo_2 & " AS 2, " & Me.txt_value & " AS txt;"
DoCmd.RunSQL SQL

Open in new window

All 3 fields are added
0
 

Author Comment

by:lkirke
ID: 34178965
Hey again experts,

Have combined your views in relation to my question.

cbo_1 = cbo_Campaign_Region
cbo_2 = cbo_Campaign_Trial
txt_value = txt_Campaign_Date

The message appears as requested. However, it doesn't show the values from the combo box that I selected.

Secondly, a run time error occurs when the YES is selected.

Further thoughts please? :)

Regards

LK
Private Sub cmd_Create_Campaign_Click()

If MsgBox("Are you sure you would like to create this combined value of 'cbo_Campaign_Region' with 'cbo_Campaign_Trial' on the 'txt_value'?", vbYesNo, " ") = vbYes Then



Dim SQL As String

SQL = "INSERT INTO tbl_values ( cbo_Campaign_Region, cbo_Campaign_Trial, txt_Campaign_Date )SELECT " & Me.cbo_Campaign_Region & " AS 1, " & Me.cbo_Campaign_Trial & " AS 2, " & Me.txt_Campaign_Date & " AS txt;"

DoCmd.RunSQL SQL



Else

DoCmd.CancelEvent

End If



End Sub

Open in new window

0
 
LVL 9

Expert Comment

by:rfportilla
ID: 34179030
Please clarify.  You say that you want to take the values and copy them to a table.  Do you mean a table in MS Access or a table in Excel?  

Getting values from the combo boxes and text box and then verifying with the user is trivial as Ed pointed out.

als315's code is SQL for database insertion.  If this is not what you are looking for or you need help tying this together, please let use know.  
0
 
LVL 9

Expert Comment

by:rfportilla
ID: 34179055
NVM, saw new post
0
 
LVL 9

Expert Comment

by:rfportilla
ID: 34179206
Here is a code snippet for the first part.  I cleaned up Ed's code a little.  I think it will make a little more sense for what you are doing.

Private Sub button1_Click()
    If MsgBox("Are you sure you would like copy the values " & cbo_1 & " and " & cbo_2 & _
      " and " & txt_Value & "on the 'txt_value'?", vbYesNo, "Title") = vbYes Then
        MsgBox ("cbo_1: " & cbo_1 & ";  cbo_2: " & cbo_2 & ";  txt_Value: " & txt_Value)
    Else
        MsgBox ("No Copy")
    End If


End Sub


Do you already have a working connection to a database?  Or are you actually outputting to a database or to another place in Excel?
0
 
LVL 39

Expert Comment

by:als315
ID: 34179432
On what line is error? Move Dim to second line
 
Private Sub cmd_Create_Campaign_Click()
Dim SQL As String
If MsgBox("Are you sure you would like to create this combined value of 'cbo_Campaign_Region' with 'cbo_Campaign_Trial' on the 'txt_value'?", vbYesNo, " ") = vbYes Then
SQL = "INSERT INTO tbl_values ( cbo_Campaign_Region, cbo_Campaign_Trial, txt_Campaign_Date )SELECT " & Me.cbo_Campaign_Region & " AS 1, " & Me.cbo_Campaign_Trial & " AS 2, " & Me.txt_Campaign_Date & " AS txt;"
DoCmd.RunSQL SQL

Else
DoCmd.CancelEvent
End If

End Sub

Open in new window

You should get warning when query is starting
Add
docmd.setwarnings = false
 before runSQL and = true after
0
 

Author Comment

by:lkirke
ID: 34179587
Hello Experts.

rfportilla - I am outputting to a table in the database, being tbl_values
als315 - I run the code, and the error is occuring within the Docmd.RunSQL SQL

Regards

LK
0
 
LVL 39

Expert Comment

by:als315
ID: 34179638
It seems there is some error in SQL sentence. May be some fields in your DB have other names. Could you upload an example of you DB with this form and table?
You can also check SQL string in debugger. Try to copy it to query (in SQL mode) and run. Query should append one string.
0
 

Author Comment

by:lkirke
ID: 34179696
als315: Quickly before I do that, should the field names within tbl_values be of any particular format or name or should there be an ID etc etc?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Expert Comment

by:als315
ID: 34179709
If there are any index fields (not autonumber), they should also be filled.
Yo can try it manually adding new record with only your three fields filled. If it is enough, you will do it without problems.
0
 
LVL 39

Expert Comment

by:als315
ID: 34179717
I hope fields names in table are: cbo_Campaign_Region, cbo_Campaign_Trial, txt_Campaign_Date
0
 

Author Comment

by:lkirke
ID: 34181403
Experts,

Please find attached the required file. Ultimately, a UNIQUE ID should also be within the table so as well.

LK


Example-1.mdb
0
 
LVL 19

Accepted Solution

by:
MINDSUPERB earned 450 total points
ID: 34182003
Add an ID field in your tbl_value and choose autonumber as its datatype. Then use this code in the click event of the button:

Private Sub cmd_Create_Campaign_Click()
Dim db As DAO.Database, strMyValue As String, strMyValue2 As String, strMyValue3 As String
If MsgBox("Are you sure you would like to create this combined value of 'cbo_Campaign_Region' with 'cbo_Campaign_Trial' on the 'txt_value'?", vbYesNo, " ") = vbYes Then
Me.txt_Campaign_Date = Me.cbo_Campaign_Region & " " & Me.cbo_Campaign_Trial

strMyValue = Me.cbo_Campaign_Region
strMyValue2 = Me.cbo_Campaign_Trial
strMyValue3 = Me.txt_Campaign_Date

Set db = CurrentDb
db.Execute "Insert into tbl_Values (cbo_campaign_region, cbo_campaign_trial, txt_campaign_date) VALUES ('" & strMyValue & "','" & strMyValue2 & "','" & strMyValue3 & "')"

Set db = Nothing


Else
DoCmd.CancelEvent
End If

End Sub

Sincerely,
Ed

Example-1.mdb
0
 
LVL 39

Expert Comment

by:als315
ID: 34182284
Change SQL string to:
0
 
LVL 39

Expert Comment

by:als315
ID: 34182288
Sorry, code was not attached.
SQL = "INSERT INTO tbl_values ( cbo_Campaign_Region, cbo_Campaign_Trial, txt_Campaign_Date )SELECT [Forms]![frm_Campaign]![cbo_Campaign_Region] AS 1, [Forms]![frm_Campaign]![cbo_Campaign_Trial] AS 2, [Forms]![frm_Campaign]![txt_Campaign_Date] AS txt;"

Open in new window

0
 

Author Comment

by:lkirke
ID: 34182304
Great Ed, works perfectly, although took out the part combining the value for the Me.txt_Campaign_Date after the Then statement.

Two small questions:
1) How can I populate the Message with the currently selected values i.e. "Are you sure you would like to create this combined value of Sydney with Wine on the 12/08/2010?
2) Before the DoCmd.Cancel Event, clear the values in the combo boxes?

Regards

LK
Private Sub cmd_Create_Campaign_Click()

Dim db As DAO.Database, strMyValue As String, strMyValue2 As String, strMyValue3 As String

If MsgBox("Are you sure you would like to create this combined value of 'cbo_Campaign_Region' with 'cbo_Campaign_Trial' on the 'txt_value'?", vbYesNo, " ") = vbYes Then



strMyValue = Me.cbo_Campaign_Region

strMyValue2 = Me.cbo_Campaign_Trial

strMyValue3 = Me.txt_Campaign_Date



Set db = CurrentDb

db.Execute "Insert into tbl_Values (cbo_campaign_region, cbo_campaign_trial, txt_campaign_date) VALUES ('" & strMyValue & "','" & strMyValue2 & "','" & strMyValue3 & "')"



Set db = Nothing





Else

DoCmd.CancelEvent

End If



End Sub

Open in new window

0
 
LVL 19

Assisted Solution

by:MINDSUPERB
MINDSUPERB earned 450 total points
ID: 34182314
Try to use this line istead:

If MsgBox("Are you sure you would like to create this combined value of " & Me.cbo_Campaign_Region & " with " & Me.cbo_Campaign_Trial & " on the 'txt_value'?", vbYesNo, " ") = vbYes Then

Ed
0
 
LVL 39

Assisted Solution

by:als315
als315 earned 50 total points
ID: 34182316
Clear values:
Me.cbo_Campaign_Region = ""
etc.
You can create custom message with any text:
Dim MsgText as string
MsgText = "Insert into tbl_Values " & Me.cbo_Campaign_Region &" ;" & Me.cbo_Campaign_Trial & " ;" & Me.cbo_Campaign_Date

If MsgBox(MsgText, vbYesNo, " ") = vbYes Then
0
 
LVL 19

Expert Comment

by:MINDSUPERB
ID: 34182320
And before the DoCmd.CancelEvent place this:

Me.cbo_Campaign_Region = Null
Me.cbo_Campaign_Trial = Null

Ed
0
 

Author Closing Comment

by:lkirke
ID: 34182336
Thank you to all Experts for the answer. Awarded majority to MINDSUPERB has it answered the question most efficiently.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now