473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Converting SQL to VBA

10 Byte
Hello, I have a test update query that I created and need to convert it to VBA - SQL so I can run it using a button in my form plus I need to learn this anyway... First is there a website that explains this? Second I have trouble with things like referring to controls on a form like:

SupervisorID =[forms]![frm_UpdateSupInfo]![txtSupervisorID]

Im using this behind my form so can I use Me.txtSupervisorID or do I have to do the whole thing?

TEST Query SQL that works:
Expand|Select|Wrap|Line Numbers
  1. UPDATE tbluEmployees SET tbluEmployees.SupervisorID = [Forms]![frm_UpdateSupInfo]![cboSupervisor]
  2. WHERE (((tbluEmployees.SupervisorID)=[forms]![frm_UpdateSupInfo]![txtSupervisorID]));
  3.  

What I came up with:
Expand|Select|Wrap|Line Numbers
  1. Dim strSQL As String
  2.  
  3. strSQL = " UPDATE tbluEmployees SET tbluEmployees.SupervisorID = [Forms]![frm_UpdateSupInfo]![cboSupervisor] "
  4. strSQL = strSQL & "WHERE (((tbluEmployees.SupervisorID)=[forms]![frm_UpdateSupInfo]![txtSupervisorID]))"
  5.   'verify the SQL works
  6.  Debug.Print strSQL
  7.  
  8. 'CurrentDb.Execute strSQL
Sep 9 '21 #1

✓ answered by cactusdata

When building SQL in VBA, you need to concatenate the values:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tbluEmployees SET tbluEmployees.SupervisorID = " & [Forms]![frm_UpdateSupInfo]![cboSupervisor] & " "
  2. strSQL = strSQL & "WHERE tbluEmployees.SupervisorID = " & [Forms]![frm_UpdateSupInfo]![txtSupervisorID] & ""
or, if frm_UpdateSupInfo is the current form (Me):

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tbluEmployees SET tbluEmployees.SupervisorID = " & Me![cboSupervisor].Value & " "
  2. strSQL = strSQL & "WHERE tbluEmployees.SupervisorID = " & Me![txtSupervisorID].Value & ""

5 5666
NeoPa
32,556 Expert Mod 16PB
May I suggest you start again at the beginning and assume we don't already know what you're thinking. After that, and before posting, read it back to yourself to check it makes sense.

SoggyCashew:
Im using this behind my form so can I use Me.txtSupervisorID or do I have to do the whole thing?
We only allow one question per thread so I'll answer this one here (as it actually makes sense). Re-do the rest elsewhere (Another thread.) as described above.

Yes. Within the module of the Form, the associated Form can always be referenced more simply as Me. Thus Me.txtSupervisorID would make sense based on a Control named [txtSupervisorID].

That said, and we can deal with that in more detail when we have a question that actually makes sense, this is only within the context of the VBA code itself. It won't be recognised within a string and it certainly won't be referenced by any SQL you build - even if the building is done within the VBA.
Sep 10 '21 #2
cactusdata
214 Expert 128KB
When building SQL in VBA, you need to concatenate the values:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tbluEmployees SET tbluEmployees.SupervisorID = " & [Forms]![frm_UpdateSupInfo]![cboSupervisor] & " "
  2. strSQL = strSQL & "WHERE tbluEmployees.SupervisorID = " & [Forms]![frm_UpdateSupInfo]![txtSupervisorID] & ""
or, if frm_UpdateSupInfo is the current form (Me):

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tbluEmployees SET tbluEmployees.SupervisorID = " & Me![cboSupervisor].Value & " "
  2. strSQL = strSQL & "WHERE tbluEmployees.SupervisorID = " & Me![txtSupervisorID].Value & ""
Sep 10 '21 #3
isladogs
456 Expert Mod 256MB
Agree with using the Me. notation as the code will be used in a form event.

You may find my SQL to VBA converter useful. See SQL to VBA and back again
Attached Files
File Type: zip SQL2VBA.zip (62.5 KB, 55 views)
Sep 10 '21 #4
soggycashew
10 Byte
Thanks all here is what I used...

Expand|Select|Wrap|Line Numbers
  1. strSQL = "UPDATE tbluEmployees SET [SupervisorID]=" & [Forms]![frm_UpdateSupInfo]![cboGainingSupervisor]
  2.     strSQL = strSQL & " WHERE [SupervisorID]=" & [Forms]![frm_UpdateSupInfo]![txtSupervisorID]
Sep 13 '21 #5
NeoPa
32,556 Expert Mod 16PB
Hi SoggyCashew.

From your last post it's more clear what you're after. While there's no doubt your code will work as it is, it does seem as if you haven't quite got what we were trying to say about the use of Me.

Here are a couple of examples that use Replace() and the more standard simple string concatenation approaches :
Expand|Select|Wrap|Line Numbers
  1. With Me
  2.     strSQL = "UPDATE [tbluEmployees] " _
  3.            & "SET    [SupervisorID]=%T " _
  4.            & "WHERE  ([SupervisorID]=%F)"
  5.     strSQL = Replace(strSQL, "%F", .txtSupervisorID)
  6.     strSQL = Replace(strSQL, "%T", .cboGainingSupervisor)
  7. End With
Expand|Select|Wrap|Line Numbers
  1. With Me
  2.     strSQL = "UPDATE [tbluEmployees] " _
  3.            & "SET    [SupervisorID]=" & .cboGainingSupervisor & " " _
  4.            & "WHERE  ([SupervisorID]=" & .txtSupervisorID & ")"
  5. End With
Where the simple dot (.) method is used that is because of the With context set up. If you skipped the With Me & End With lines then these would simply need to be done explicitly as Me.cboGainingSupervisor & Me.txtSupervisorID.

NB. While it may seem strange to add the With lines as it creates more code, it does actually save on preparing the object for use. More important in code where there are a number of object references to process which can all be saved by using that approach.

I hope that helps you to understand more clearly what we've been telling you.
Sep 13 '21 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: mustafa | last post by:
Dear sir , I have built my application in visual basic 6.0 and crystal Report8.5 , Now i migrated my application to VB.net using the upgrade wizard.My visual basic form is upgraded to vb.net...
29
by: Armand Karlsen | last post by:
I have a website ( http://www.zen62775.zen.co.uk ) that I made HTML 4.01 Transitional and CSS compliant, and I'm thinking of converting it into XHTML to learn a little about it. Which XHTML variant...
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
3
by: Mary | last post by:
Hi, Does anyone know of any software out there that would convert an application written in VBScript to either VB.NET or C#/C++ quite quickly for me, or will I have to re-write the application...
2
by: Map Reader | last post by:
Greetings, I am converting an old VB6 application to use .NET. One of the old controls loads icons from the disk and displays them. However, the transparent color turns to blue somewhere in the...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.