473,385 Members | 1,342 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,385 software developers and data experts.

Setting ControlSource in VBA (Syntax Problem)

doma23
107 100+
I have an If-Else condition in VBA. I want to do next:

Expand|Select|Wrap|Line Numbers
  1. If (Not IsNull(Me.cmbSubstantive)) Then   
  2.     Me!txtSub_Artikel.ControlSource = DLookup("[Artikel]", "tblSubstantive", "[Substantiv] = '" & [Forms]![frmMain]![cmbSubstantive] & "'")
  3.  
  4. Else
  5.     Me.txtSub_Artikel.ControlSource = ""
  6. End If
However, I'm having a problem with the syntax of DLookup when being assigned through VBA.
The problem is the same as if I try to put this Dlookup code into a string variable.

The error I'm getting is "Run-Time Error 13: Type mismatch".

Thanks!
Sep 26 '11 #1
6 3055
doma23
107 100+
Ok, I've changed the syntax to this:

Expand|Select|Wrap|Line Numbers
  1. Me.txtSub_Artikel.ControlSource = "=DLookup(""[Artikel]""; ""tblSubstantive""; ""[Substantiv] = '"" & [Forms]![frmMain]![cmbSubstantive] & ""'"")"
Now it gives me "run-time error 2438: the expression you entered contains invalid syntax".
Sep 26 '11 #2
doma23
107 100+
Ok, I've solved it by using DLookup function directly from the VBA:

Expand|Select|Wrap|Line Numbers
  1.     Me.txtSub_Artikel.Value = DLookup("[Artikel]", "tblSubstantive", "[Substantiv] = '" & [Forms]![frmMain]![cmbSubstantive] & "'")
Although, still be interested to know how to assign the DLookup formula to ControlSource property of a text box.
Sep 26 '11 #3
nico5038
3,080 Expert 2GB
In general I try to minimize the use of DLookup() and the other D...() functions as they are slow.
For getting related data from a combobox selection into an unbound text field it's easier to add the field to the combo's ControlSource and suppress the column by giving the rowwidth the value 0 (zero).
Now the AfterUpdate event can be used to fill the textfield like:
Expand|Select|Wrap|Line Numbers
  1. Me.txtSub_Artikel = Me.cmbSubstantive.Column(1)
  2.  
This assumes that the combo's rowsource query looks like:
Expand|Select|Wrap|Line Numbers
  1. select ArticleCode, Article from tblArticle
  2.  
The column number is starting with 0 (zero) so the (1) will give the Article column.

Nic;o)
Sep 26 '11 #4
NeoPa
32,556 Expert Mod 16PB
This is absolutely not an Insight. I will move it to the Answers forum.
Sep 26 '11 #5
doma23
107 100+
@nico5038 - tnx, seems like a good idea.
@NeoPa - sorry, I don't know how it ended up in insights.
Sep 27 '11 #6
NeoPa
32,556 Expert Mod 16PB
Doma:
sorry, I don't know how it ended up in insights.
No worries this time Doma.

Next time you see "Share an insight, write an article" in big bold letters at the top of the page though, you should realise you're not in the right place to post a question ;-)

I'm pleased you found a solution to your problem anyway :-)
Sep 27 '11 #7

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

Similar topics

4
by: Toonman | last post by:
I'm trying to use a couple of variables in a stored procedure. Things work fine when I hard code the data into the variables and also work fine when I use the variable in the WHERE clause and hard...
8
by: dave | last post by:
I am sending an email from an asp.net application. I have set the from address a b@b.com but would like the name of this user to show up as Big Bob. How can i tell .net email object that when...
2
by: Dominik | last post by:
Hello. I hope I have choosed right group. I have problem preparing SQL syntax. I think it is quite simple but I haven't tried to do this till now. The thing is that I have a string and I must...
2
by: Adam Smith | last post by:
Syntax problem Link works w/ Netscape <a href="passwdhlp.html"> <button type="button" name="Help" style="font: 11pt arial bold; background white;color:red">Help!</button> </a> But not...
17
by: Paul | last post by:
HI! I am trying to dynamically add content into Div tags with the use of JavaScript. it loads the page but adds a few characters. the script is below. <script language="JavaScript">...
3
by: PA | last post by:
Hi All, I am not new to databases by to db2 indeed yes...and I am finding significant procedural limitations in DB2 triggers when compared to procedures...this becomes more limiting when you try...
6
by: Andrew Mueller | last post by:
Hello all, I am working on a project, converting code from VB 6.0 to C#. I have a COM object (SDK.dll) referenced and am having some issues using it. I don't think it is specific to the object...
2
by: martinharvey via DotNetMonster.com | last post by:
I would be very grateful if someone could help me with a stored procedure syntax problem I want to insert the value "OrderTotal" into databasetable("Newtable") column "OrderTotal" (money (8))....
12
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select...
2
by: Nick AU | last post by:
Hello, I am using MS Access 2003 and trying to set a textbox's ControlSource in VBA code. this following works fine Task.ControlSource = "." However, if i try to set the control source...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.