Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-19
22
Medium Priority
?
453 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 40

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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 40

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 40

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
 
LVL 40

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 40

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 1800 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 40

Expert Comment

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

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 1800 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 40

Assisted Solution

by:als315
als315 earned 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

705 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